com.notuvy.thread
Class ContinuousExecution

java.lang.Object
  extended by com.notuvy.thread.ContinuousExecution
All Implemented Interfaces:
ThreadControllable

public class ContinuousExecution
extends Object
implements ThreadControllable

Basically an infinite loop with a sleep period (to avoid CPU saturation). It is used to continuously execute an action (Runnable) in a separate Thread. Once started, it can be suspended (disabled), resumed (enabled), and stopped.

It necessarily needs to operate in a separate thread to use the rescheduling feature of ExecutorService. To make the launching thread wait for execution to finish cleanly (simulate synchronous launching), do:

   ContinuousExecution ce = new ContinuousExecution(period, runnable, threadName);
   ce.start().waitForTermination();
 

Author:
murali

Field Summary
protected static org.apache.log4j.Logger LOG
           
 
Constructor Summary
ContinuousExecution(int pPeriod, Runnable pRunnable, String pThreadName)
           
ContinuousExecution(long pPeriod, Runnable pRunnable, String pThreadName)
           
 
Method Summary
 void changePeriod(int pNewPeriod)
          Dynamically change the wait period.
 void changePeriod(long pNewPeriod)
           
 ThreadControl getControl()
           
 int getPeriod()
           
static void main(String[] args)
           
 ContinuousExecution start()
           
 ContinuousExecution start(boolean pRunImmediately)
           
 String toString()
           
 void triggerNow()
          If the thread is sleeping, force it to run now.
 void waitForTermination()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LOG

protected static final org.apache.log4j.Logger LOG
Constructor Detail

ContinuousExecution

public ContinuousExecution(int pPeriod,
                           Runnable pRunnable,
                           String pThreadName)

ContinuousExecution

public ContinuousExecution(long pPeriod,
                           Runnable pRunnable,
                           String pThreadName)
Method Detail

getPeriod

public int getPeriod()

getControl

public ThreadControl getControl()
Specified by:
getControl in interface ThreadControllable

start

public ContinuousExecution start()

start

public ContinuousExecution start(boolean pRunImmediately)

triggerNow

public void triggerNow()
If the thread is sleeping, force it to run now.


changePeriod

public void changePeriod(int pNewPeriod)
Dynamically change the wait period. If work is overdue (based on the new value) then the work should trigger immediately.

Parameters:
pNewPeriod - The wait period in milliseconds.

changePeriod

public void changePeriod(long pNewPeriod)

waitForTermination

public void waitForTermination()
                        throws InterruptedException
Throws:
InterruptedException

toString

public String toString()
Overrides:
toString in class Object

main

public static void main(String[] args)


Copyright © 2013. All Rights Reserved.