org.clazzes.util.thread
Class ThreadStopper

java.lang.Object
  extended by org.clazzes.util.thread.ThreadStopper

public class ThreadStopper
extends java.lang.Object

Author:
wglas this class is used in threads, which periodically check for an external request for terminating it's work.

Constructor Summary
ThreadStopper()
          Instantiates a new ThreadStopper instance, which is in the not-being-stopped state.
 
Method Summary
 boolean isToBeFinished()
          This method is used inside the thread's main loop in order to check, whether the thread has be stopped the next time it has finished a portion of his work.
 void requestStop()
          Requests the thread to be stopped.
 void resume()
          Requests the thread to be stopped.
 void waitToBeFinished()
           
 boolean waitToBeFinished(long timeout)
          This function waits up to the given timeout in milliseconds or exits immediately after requestStop() has been called.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadStopper

public ThreadStopper()
Instantiates a new ThreadStopper instance, which is in the not-being-stopped state.

Method Detail

isToBeFinished

public boolean isToBeFinished()
This method is used inside the thread's main loop in order to check, whether the thread has be stopped the next time it has finished a portion of his work.

Returns:
true, if we should leave the thread loop now.

waitToBeFinished

public boolean waitToBeFinished(long timeout)
                         throws java.lang.InterruptedException
This function waits up to the given timeout in milliseconds or exits immediately after requestStop() has been called. In the later case, the function returns true.

Parameters:
timeout - The maximal number of milliseconds to wait.
Returns:
true, if the main loop should be left immediately.
Throws:
java.lang.InterruptedException - if some other thread calls Thread.interrupt on this Thread while waiting.

waitToBeFinished

public void waitToBeFinished()
                      throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

requestStop

public void requestStop()
Requests the thread to be stopped. Since the thread may just be doing some work, it may take some time until the thread really stops.


resume

public void resume()
Requests the thread to be stopped. Since the thread may just be doing some work, it may take some time until the thread really stops.



Copyright © 2010. All Rights Reserved.