com.marringtons.util
Class Semaphore

java.lang.Object
  extended bycom.marringtons.util.Semaphore

public class Semaphore
extends Object

This is a simple object to use as a semaphore. It handles exceptions.

 Semaphore semaphore = new Semaphore;
 
 if (noMoreToDo) semaphore.pause();	// only returns on a start() from another thread
 
 // or from a thread.run(), process once a second or if asked to...
 while (true)
   {
     processWork();
     if (semaphore.pause( 1000))
       Log.message( "Process paused for 1 second");
     else
       Log.message( "Process resumed on command in less than 1 second");
   }
 semaphone.pause();	// only continue on a resume - otherwise wait forever.
 
 // and from another thread
 if (workToDo) semaphore.resume();
 
If you don't need a special semaphore object, use Application.pause() and resume()

Author:
Paul Marrington
See Also:
Application

Constructor Summary
Semaphore()
           
 
Method Summary
 void clear()
          Clear any outstanding resumes rather than queue them up.
 void disable()
          Neither pause nor resume will actually do anything.
 boolean pause()
          Pause the running thread.
 boolean pause(int timeout)
          Pause the running thread.
 void reenable()
          Neither pause nor resume will actually do anything.
 void resume()
          resume a thread that has been paused by a call on this object.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Semaphore

public Semaphore()
Method Detail

pause

public boolean pause()
Pause the running thread.

Returns:
false if the pause was interrupted by other than an unlock

pause

public boolean pause(int timeout)
Pause the running thread.

Parameters:
timeout - maximum milliseconds to remain locked.
Returns:
false if the pause was interrupted by other than resume() - usually timeout

resume

public void resume()
resume a thread that has been paused by a call on this object.


clear

public void clear()
Clear any outstanding resumes rather than queue them up.


disable

public void disable()
Neither pause nor resume will actually do anything. Use reenable() to re-enable. Mostly used by unit tests when they don't want a daemon to cut in and confuse the results.


reenable

public void reenable()
Neither pause nor resume will actually do anything. Use reenable() to re-enable. Mostly used by unit tests when they don't want a daemon to cut in and confuse the results.



Copyright © 2005 Paul Marrington http://library.marringtons.com