public class Counter extends Object
waitForZero(long)
method.
The main usage of the class is to allow a master thread, to know when other
threads (slaves) have passed a certain point in execution.
As opposed to a Barrier or a Semaphore, this class should be used only
with 1 waiting thread (a master) and any number of slave threads.
Thread 1: counter.increment(); thread2.start(); counter.increment(); thread3.start(); // wait 1 second for other threads to complete counter.waitForZero(1000); Thread 2: // do some work counter.decrement(); Thread 3: // do some work counter.decrement();Mainly for usage inside the framework. All methods are thread-safe however for the master/slave pattern, synchronized blocks are recommended as multiple operations have to be executed at once.
Constructor and Description |
---|
Counter(String name)
Create counter with a given name.
|
Modifier and Type | Method and Description |
---|---|
void |
decrement()
Decrement the counter value.
|
boolean |
decrementAndWait(long timeToWait) |
int |
getValue()
Return the counter value.
|
void |
increment()
Increment the counter value.
|
boolean |
is(int value) |
boolean |
isZero()
Check if the counter value is zero.
|
String |
toString() |
boolean |
waitFor(int value,
long waitTime)
Wait maximum the given amount of time, for the counter to reach the given
value.
|
boolean |
waitForZero(long waitTime)
Specialized method which waits for 0.
|
public Counter(String name)
name
- counter namepublic void increment()
public void decrement()
public boolean decrementAndWait(long timeToWait)
public boolean isZero()
public boolean is(int value)
public int getValue()
public boolean waitForZero(long waitTime)
waitTime
- waitFor(int, long)
public boolean waitFor(int value, long waitTime)
Object.wait(long)
and
Object.notify()
mechanism to work appropriately. Please see the
class javadoc for more info.
This method will stop waiting and return true if the thread is
interrupted.value
- the value to wait forwaitTime
- the time (in miliseconds) to wait for zero valueCopyright © 2006–2015. All rights reserved.