cz.cuni.utils
Class RunnableTemplate

java.lang.Object
  extended by cz.cuni.utils.RunnableTemplate
All Implemented Interfaces:
java.lang.Runnable

public class RunnableTemplate
extends java.lang.Object
implements java.lang.Runnable

This class shouldn't be altered or instancied - it's only example / template where you can learn how to implement runnable classes and what is a good practice when doing so.


Field Summary
private  Flag<java.lang.Boolean> communicationAlive
          Always keep track whether the communication is up and running!
private  java.util.concurrent.CountDownLatch communicationLatch
          If you want to start communication with somebody - always use this object.
private  java.lang.Thread thread
          If instance has been started (via runThread()) - then this is initialized.
private  Flag<java.lang.Boolean> threadAlive
          Flag which tells us wether threadIsAlive - initialize the flag at the beggining of the run() and drops it as the last command of the run().
 
Constructor Summary
RunnableTemplate()
           
 
Method Summary
protected  void communicationDied()
          Mark the communication as dead.
protected  void communicationSettedUp()
          Mark the communication is setted up.
 Flag<java.lang.Boolean> getCommunicationAliveFlag()
           
 java.lang.Thread getThread()
          Get thread of the instance, if not isThreadAlive() then it returns null.
 Flag<java.lang.Boolean> getThreadAliveFlag()
           
 boolean isCommunicationAlive()
          Returns state of the communication.
 boolean isThreadAlive()
          Returns state of the thread.
 void run()
          Is called internaly from runThread() ... this class shouldn't be run via Thread.run(object).
 java.lang.Thread runThread()
          This method should be used to run() this class in a new thread.
 boolean waitForCommunication()
          This operation is guaranteed to block until communication is setted up.
 boolean waitForCommunication(long timeOut, java.util.concurrent.TimeUnit unit)
          This operation is guaranteed to block until communication is setted up.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threadAlive

private Flag<java.lang.Boolean> threadAlive
Flag which tells us wether threadIsAlive - initialize the flag at the beggining of the run() and drops it as the last command of the run(). We assume that you may have only one thread running with one instance of the class.


communicationLatch

private java.util.concurrent.CountDownLatch communicationLatch
If you want to start communication with somebody - always use this object. It's from java.lang.concurrent and if any other thread want to wait for the signal that communication is setted up may call waitForCommunication(). When doing anything with communicationLatch, always do it in sychronized(communicationLatch) if you're going to reinitialize this - always do communicationLatch.countDown() to release all threads waiting on it! check out methods: waitForCommunication(), communicationSettedUp, communicationDied()


communicationAlive

private Flag<java.lang.Boolean> communicationAlive
Always keep track whether the communication is up and running! Correctly handle this flag inside your code.


thread

private java.lang.Thread thread
If instance has been started (via runThread()) - then this is initialized. If you want to be sure that thread != null - do it in synchronized(thread)

Constructor Detail

RunnableTemplate

public RunnableTemplate()
Method Detail

getThreadAliveFlag

public Flag<java.lang.Boolean> getThreadAliveFlag()

isThreadAlive

public boolean isThreadAlive()
Returns state of the thread. (Anybody may check whether thread is running.)

Returns:
threadAlive flag

getCommunicationAliveFlag

public Flag<java.lang.Boolean> getCommunicationAliveFlag()

isCommunicationAlive

public boolean isCommunicationAlive()
Returns state of the communication. (Anybody may check whether communication is OK.)

Returns:
communicationAlive flag

waitForCommunication

public boolean waitForCommunication()
This operation is guaranteed to block until communication is setted up. NOTE: when the method is finished (you pass through the latch) you still has to test isCommunicationAlive(), if not - call this method again! That's because you may be freed because of reinitialization of the latch or because of interrupt exception.


waitForCommunication

public boolean waitForCommunication(long timeOut,
                                    java.util.concurrent.TimeUnit unit)
This operation is guaranteed to block until communication is setted up. NOTE: when the method is finished (you pass through the latch) you still has to test isCommunicationAlive(), if not - call this method again! That's because you may be freed because of reinitialization of the latch or because of interrupt exception.


communicationSettedUp

protected void communicationSettedUp()
Mark the communication is setted up. Raises the communicationAlive flag and free threads awaiting on the latch.


communicationDied

protected void communicationDied()
Mark the communication as dead. Drops the communicationAlive flag and reinitialize communicationLatch (correctly - freeing all the waiting threads and reinitilize .. threads supposed to wait on communicationAlive flag!).


runThread

public java.lang.Thread runThread()
This method should be used to run() this class in a new thread. Note that we assume that each instance may be run only in one thread, so this method will return new running thread or null. Note: if you want to wait till old thread dies weither do instance.getThread().interrupt() or instance.getThread().join() which will wait till thread dies().

Returns:
new running Thread or null

getThread

public java.lang.Thread getThread()
Get thread of the instance, if not isThreadAlive() then it returns null.


run

public void run()
Is called internaly from runThread() ... this class shouldn't be run via Thread.run(object). We assume that each object, can be run in ONLY ONE thread. 1) therefore as the first action, test wether threadIsAlive - if so, exit 2) do all the work in try/catch block and catch all the exceptions, otherwise you won't be able to do the 3) 3) as the last action (almost), drops the flag threadAlive

Specified by:
run in interface java.lang.Runnable