public class RunningEventSwitch extends Object
This is a helper class for coordinating a "restartable" job. A "restartable"
job is one that can be started over before it's finished but there should only
be on running at a time. To use it you should instantiate a RunningEventSwitch
.
When you execute a job in another thread:
workerRunning()
.workerFinished()
.wasPreempted()
and if it ever returns 'true'
the worker should exit.waitForWorkerToStart()
.preemptWorkerAndWait()
,
and then it can restart the job using a new Thread.The RunningEventSwitch can also monitor an externally supplied "isRunning" flag so that the job will be 'preempted' whenever that's also set to false
Constructor and Description |
---|
RunningEventSwitch() |
RunningEventSwitch(AtomicBoolean externalIsRunning) |
Modifier and Type | Method and Description |
---|---|
void |
preemptWorkerAndWait()
The main thread uses this method when it needs to preempt the worker and
wait for the worker to finish before continuing.
|
void |
waitForWorkerToStart()
This allows the main thread to wait until the worker is started in order
to continue.
|
boolean |
wasPreempted()
The worker thread can use this method to check if it's been explicitly preempted.
|
void |
workerFinished()
The worker thread should indicate that it's done in a finally clause on it's way
out.
|
void |
workerRunning()
This is called from the worker thread to notify the fact that
it's been started.
|
public RunningEventSwitch(AtomicBoolean externalIsRunning)
public RunningEventSwitch()
public void workerRunning()
public boolean wasPreempted()
public void workerFinished()
public void preemptWorkerAndWait()
public void waitForWorkerToStart()
Copyright © 2018. All rights reserved.