com.vodafone360.people.service
Class WorkerThread

java.lang.Object
  extended by java.lang.Thread
      extended by com.vodafone360.people.service.WorkerThread
All Implemented Interfaces:
java.lang.Runnable

public class WorkerThread
extends java.lang.Thread

The worker thread is the main thread of execution within the People Client service. It is responsible for running engines via the EngineManager The WorkerThread remains active while there are engines with pending run times, becoming dormant otherwise The WorkerThread can be woken up when a request is issued or via an Alarm set to trigger an engine runtime update check It is extremely important that the WorkerThread does not become blocked.


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
static java.lang.String ALARM_WORKER_THREAD
          Name of alarm for waking up worker thread
private static int FORCE_PAUSE_MS
          Period of time to put worker thread to sleep after it has run MAX_RUNS_WITHOUT_PAUSE times without any pause.
private static long LOOP_SLEEP_TIME_MS
          Small wait time before looping around the engines again
private static int MAX_RUNS_WITHOUT_PAUSE
          Maximum loop iterations before allowing the worker thread to sleep for a while to preserve battery.
private  EngineManager mEngineManager
          Reference to the EngineManager.
private  Handler mHandler
          Handler used so that threads can wake up the worker thread by sending a message.
private  java.lang.Boolean mLoopOneMoreTime
          Set to true when any of the engines run at least once.
private  boolean mShutdownThread
          Set to true when the worker thread is about to stop running.
private  java.lang.Object mWakeLock
          Object is used to control the worker thread.
private static java.lang.String WORKER_THREAD_NAME
          Worker thread is renamed to this string
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
protected WorkerThread(Handler handler)
           
 
Method Summary
protected  void close()
          Shut down and join the running thread.
 void run()
          Loop through all the engines until no more work can be done right now, then set an Alarm if more work is required.
private  void setWakeupTime(long nextRunTime)
          Wake up the WorkerThread at the given time.
private  void threadLoopSleep()
          Pause the thread between cycles to stop it overloading the device and possibly blocking the UI.
protected  boolean wakeUp()
          Wake the worker thread.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALARM_WORKER_THREAD

public static final java.lang.String ALARM_WORKER_THREAD
Name of alarm for waking up worker thread

See Also:
Constant Field Values

WORKER_THREAD_NAME

private static final java.lang.String WORKER_THREAD_NAME
Worker thread is renamed to this string

See Also:
Constant Field Values

LOOP_SLEEP_TIME_MS

private static final long LOOP_SLEEP_TIME_MS
Small wait time before looping around the engines again

See Also:
Constant Field Values

MAX_RUNS_WITHOUT_PAUSE

private static final int MAX_RUNS_WITHOUT_PAUSE
Maximum loop iterations before allowing the worker thread to sleep for a while to preserve battery.

See Also:
Constant Field Values

FORCE_PAUSE_MS

private static final int FORCE_PAUSE_MS
Period of time to put worker thread to sleep after it has run MAX_RUNS_WITHOUT_PAUSE times without any pause. TODO: Make this stricter as the code base improves.

See Also:
Constant Field Values

mWakeLock

private final java.lang.Object mWakeLock
Object is used to control the worker thread. When no work is needed the thread will wait on this object.


mEngineManager

private EngineManager mEngineManager
Reference to the EngineManager.


mHandler

private Handler mHandler
Handler used so that threads can wake up the worker thread by sending a message.


mLoopOneMoreTime

private java.lang.Boolean mLoopOneMoreTime
Set to true when any of the engines run at least once.


mShutdownThread

private boolean mShutdownThread
Set to true when the worker thread is about to stop running.

Constructor Detail

WorkerThread

protected WorkerThread(Handler handler)
Parameters:
context - Context Android Application Context.
Method Detail

wakeUp

protected boolean wakeUp()
Wake the worker thread.

Returns:
TRUE if thread is awakened successfully.

run

public void run()
Loop through all the engines until no more work can be done right now, then set an Alarm if more work is required.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

threadLoopSleep

private void threadLoopSleep()
Pause the thread between cycles to stop it overloading the device and possibly blocking the UI.


setWakeupTime

private void setWakeupTime(long nextRunTime)
Wake up the WorkerThread at the given time.

Parameters:
set - TRUE sets the restart time, FALSE cancel any pending restarts.

close

protected void close()
Shut down and join the running thread.



JavaDoc