Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.eclipse.gemini.blueprint.extender.internal.util.concurrent
Class Counter

    java.lang.Object
      org.eclipse.gemini.blueprint.extender.internal.util.concurrent.Counter

    public class Counter

extends Object

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.

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.

Author:
  Costin Leau

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

### Counter

    public Counter(String name)

  Create counter with a given name.

  Parameters:
    `name` - counter name

Method Detail

### increment

    public void increment()

  Increment the counter value.

### decrement

    public void decrement()

  Decrement the counter value.

### decrementAndWait

    public boolean decrementAndWait(long timeToWait)

### isZero

    public boolean isZero()

  Check if the counter value is zero.

  Returns:
    true if value is equal or below zero, false otherwise.

### is

    public boolean is(int value)

### getValue

    public int getValue()

  Return the counter value.

  Returns:
    the counter value.

### toString

    public String toString()

  Overrides:
    `toString` in class `Object`

### waitForZero

    public boolean waitForZero(long waitTime)

  Specialized method which waits for 0. Identical to waitFor(0, waitTime).

  Parameters:
    `waitTime` -
  Returns:
    true if the waiting timed out, false otherwise
  See Also:
    `waitFor(int, long)`

### waitFor

    public boolean waitFor(int value,
                           long waitTime)

  Wait maximum the given amount of time, for the counter to reach the given value. This mechanism relies on `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.

  Parameters:
    `value` - the value to wait for
    `waitTime` - the time (in miliseconds) to wait for zero value
  Returns:
    true if the waiting timed out, false otherwise

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2006-2013. All Rights Reserved.