|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.eclipse.gemini.blueprint.extender.internal.util.concurrent.Counter
public class Counter
Simple counting class which can be incremented or decremented in a
synchronized manner. This class can be used as a synchronization mechanism
between threads mainly though 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.
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 Summary | |
---|---|
Counter(String name)
Create counter with a given name. |
Method Summary | |
---|---|
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public Counter(String name)
name
- counter nameMethod Detail |
---|
public void increment()
public void decrement()
public boolean decrementAndWait(long timeToWait)
public boolean isZero()
public boolean is(int value)
public int getValue()
public String toString()
toString
in class Object
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 value
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |