public final class PoisonRunnable extends Object implements Runnable
BlockingQueue
, null is an invalid value. However,
if several threads have been started that are simply reading from the queue for
jobs, we want to let the threads know when to stop. Polling can not be used since
the BlockingQueue.take()
operations blocks until a value is added. If
the queue is a queue of Runnable
jobs, this class solves the problem.
An instanceof check can be done on the returned runnable. If it is a poisoned
one, the worker knows to stop consuming and terminate.
run()
method does not perform any action. The
poison runnable can be used to perform ending clean up work, such as posting
to a semaphore, by giving the work job to the
PoisonRunnable(java.lang.Runnable)
constructor. The runnable given
will be run when the Poison runnable's run method is called.Constructor and Description |
---|
PoisonRunnable()
Creates a new PoisonRunnable that will do nothing when its run method is called.
|
PoisonRunnable(CountDownLatch latch)
Creates a new PoisonRunnable that will call the
CountDownLatch.countDown() method on the given latch when its
run method is finally called. |
PoisonRunnable(CyclicBarrier barrier)
Creates a new PoisonRunnable that will call the
CyclicBarrier.await() method of the given barrier when its run
method is finally called. |
PoisonRunnable(Runnable lastStep)
Creates a new PoisonRunnable that will run the given runnable when it is called.
|
public PoisonRunnable(Runnable lastStep)
lastStep
- the runnable to call when this runnable is finally called.public PoisonRunnable(CountDownLatch latch)
CountDownLatch.countDown()
method on the given latch when its
run method is finally called.latch
- the latch to decrementpublic PoisonRunnable(CyclicBarrier barrier)
CyclicBarrier.await()
method of the given barrier when its run
method is finally called.barrier
- the barrier to wait on.public PoisonRunnable()
Copyright © 2017. All rights reserved.