Class WorkerThread
- java.lang.Object
-
- com.jogamp.common.util.WorkerThread
-
public class WorkerThread extends Object
A re-#start()
'able,pause(boolean)
'able and interrupt'able workerthread
with an optional minimum execution duration, seegetSleptDuration()
executing atask
periodically.Optionally a
state
task
can be given for fine grained control.If an exception occurs during execution of the work
WorkerThread.Callback
, the workerthread
ispause(boolean)
'ed andhasError()
as well asgetError(boolean)
can be used to query and clear the state. User mayresume()
or#stop()
the thread.If an exception occurs during execution of the optional
state
task
, the workerthread
is#stop()
'ed andhasError()
as well asgetError(boolean)
can be used to query and clear the state.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
WorkerThread.Callback
static interface
WorkerThread.StateCallback
-
Constructor Summary
Constructors Constructor Description WorkerThread(java.time.Duration minPeriod, java.time.Duration minDelay, boolean daemonThread, WorkerThread.Callback work)
Instantiates a newWorkerThread
.WorkerThread(java.time.Duration minPeriod, java.time.Duration minDelay, boolean daemonThread, WorkerThread.Callback work, WorkerThread.StateCallback stateChangeCB)
Instantiates a newWorkerThread
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Exception
getError(boolean clear)
Returns the exception ishasError()
.java.time.Duration
getMinDelay()
Returns enforced minimum work-loop-delay orDuration.ZERO
for none.java.time.Duration
getMinPeriod()
Returns enforced minimum work-loop-period orDuration.ZERO
for none.java.time.Duration
getSleptDuration()
Returns the sleptDuration
delta ofgetMinPeriod()
and consumedCallback#run()
duration, which minimum isgetMinDelay()
.Thread
getThread()
Returns the worker thread ifisRunning()
, otherwisenull
.boolean
hasError()
Returns true if an exception occured duringCallable
work execution.boolean
isActive()
Returns true if the worker threadisRunning()
and is notpaused
.boolean
isPaused()
Returns true if the worker threadisRunning()
and ispaused
.boolean
isRunning()
Returns true if the worker thread has started via#start()
and has not ended, e.g.void
pause(boolean waitUntilDone)
Pauses execution of the#start()
'ed worker thread.void
resume()
Resumes execution of thepause(boolean)
'ed worker thread.void
start(boolean paused)
Starts execution of a new worker thread if notisRunning()
, i.e.void
stop(boolean waitUntilDone)
Stops execution of the#start()
'ed worker thread.String
toString()
-
-
-
Constructor Detail
-
WorkerThread
public WorkerThread(java.time.Duration minPeriod, java.time.Duration minDelay, boolean daemonThread, WorkerThread.Callback work)
Instantiates a newWorkerThread
.- Parameters:
minPeriod
- minimum work-loop-period to throttle execution ornull
if unthrottled, seegetSleptDuration()
minDelay
- minimum work-loop-delay to throttle execution ornull
if unthrottled, seegetSleptDuration()
daemonThread
- argument forThread.setDaemon(boolean)
work
- the actual workWorkerThread.Callback
to perform.
-
WorkerThread
public WorkerThread(java.time.Duration minPeriod, java.time.Duration minDelay, boolean daemonThread, WorkerThread.Callback work, WorkerThread.StateCallback stateChangeCB)
Instantiates a newWorkerThread
.- Parameters:
minPeriod
- minimum work-loop-period to throttle execution ornull
if unthrottled, seegetSleptDuration()
minDelay
- minimum work-loop-delay to throttle execution ornull
if unthrottled, seegetSleptDuration()
daemonThread
- argument forThread.setDaemon(boolean)
work
- the actual workWorkerThread.Callback
to perform.stateChangeCB
- optionalWorkerThread.StateCallback
called at differentWorkerThread.StateCallback.State
changes while locked
-
-
Method Detail
-
start
public final void start(boolean paused)
Starts execution of a new worker thread if notisRunning()
, i.e. never#start()
'ed or#stop()
'ed.Method blocks until the new worker thread has been started and
isRunning()
andisActive()
ifpaused == false
.- Parameters:
paused
- iftrue
, keeps the new worker thread paused, otherwiseresume()
it.
-
stop
public final void stop(boolean waitUntilDone)
Stops execution of the#start()
'ed worker thread.Method blocks until worker thread has been
stopped
ifwaitUntilDone
istrue
.
-
pause
public final void pause(boolean waitUntilDone)
Pauses execution of the#start()
'ed worker thread.Method blocks until worker thread has been
isActive()
'ated ifwaitUntilDone
istrue
.
-
resume
public final void resume()
Resumes execution of thepause(boolean)
'ed worker thread.
-
isRunning
public final boolean isRunning()
Returns true if the worker thread has started via#start()
and has not ended, e.g. via#stop()
. It might bepaused
.
-
isActive
public final boolean isActive()
Returns true if the worker threadisRunning()
and is notpaused
.
-
isPaused
public final boolean isPaused()
Returns true if the worker threadisRunning()
and ispaused
.
-
hasError
public final boolean hasError()
Returns true if an exception occured duringCallable
work execution.
-
getThread
public final Thread getThread()
Returns the worker thread ifisRunning()
, otherwisenull
.
-
getError
public final Exception getError(boolean clear)
Returns the exception ishasError()
.- Parameters:
clear
- if true, clear the exception
-
getMinPeriod
public final java.time.Duration getMinPeriod()
Returns enforced minimum work-loop-period orDuration.ZERO
for none.- See Also:
getSleptDuration()
-
getMinDelay
public final java.time.Duration getMinDelay()
Returns enforced minimum work-loop-delay orDuration.ZERO
for none.- See Also:
getSleptDuration()
-
getSleptDuration
public final java.time.Duration getSleptDuration()
Returns the sleptDuration
delta ofgetMinPeriod()
and consumedCallback#run()
duration, which minimum isgetMinDelay()
.Returns
zero
forzero
getMinPeriod()
andgetMinDelay()
or exceedingCallback#run()
duration withoutgetMinDelay()
.
-
-