|
JThreadKitTM v1.1.0 ( public members only)
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--com.jthreadkit.SyncInteger
SyncInteger is used to signal int values
among two or more threads.
SyncInteger encapsulates Java's wait-notify mechanism making
it simple and safe for one thread to wait for a certain value to be set
by another thread. In fact, any number of threads can be waiting
and any number of threads can safely change the value.
For example, let's say that the value is currently 2 and
threadA comes along and waits until the value is in the
inclusive range of 5 to 8.
Meanwhile sometime soon after threadA starts waiting,
threadB comes along and sets the value to be
7, then threadA stops waiting and proceeds.
Specifically, if there's an instance of SyncInteger
constructed:
|
SyncInteger si = new |
threadA executes this code:
|
try {
si. |
threadB executes this code:
|
si. |
threadA returns from
waitUntilValueInRange(5, 8)
and continues on. If threadA calls
waitUntilValueInRange(5, 8)
and the value is already at least 5 and at most 8,
it returns right away with no delay.
InterruptedException - can be thrown from many
of the methods in this class. If one thread is blocked inside
one of these methods and another thread interrupts it,
InterruptedException is thrown by the blocked thread.
TimedOutException - can be thrown from many
of the methods in this class only if the
"use TimedOutException" option is set.
If true is passed to the
setUseTimedOutException()
method, the option is set and TimedOutException will be
thrown from methods that timeout when the specified timeout period
expires. If false is passed, the option is unset and
TimedOutException will never be thrown.
The isUseTimedOutExceptionSet() method returns true
if the option is set and TimedOutException's will be thrown.
TimedOutException is a subclass of RuntimeException
so there is no need for a try-catch block if the option is not set.
This option allows you to determine whether or not a timeout is
an exceptional condition in your context (a condition that
should result in an exception being thrown). If timeouts are routine,
you probably will want to leave this option in its default unset state.
See the class description of
TimedOutException for more information.
ShutdownException - can be thrown from many
of the methods in this class. This exception is not thrown by methods
until the shutdown() method is called. If shutdown() is
never called, this exception will never be thrown and can
be safely ignored. ShutdownException is a subclass of
RuntimeException so there is no need for a try-catch block
if shutdown() won't be called. After shutdown()
is called, any threads that are currently blocked waiting will immediately
throw this exception. In addition, any future calls to methods that would
either result in waiting or would alter the object cause
this exception to be thrown (if no waiting is needed because the condition
is already met, this exception will not be thrown).
See the class description of
ShutdownException for more information.
| Constructor Summary | |
SyncInteger()
|
|
SyncInteger(int initialValue)
|
|
SyncInteger(int initialValue,
int minValue,
int maxValue)
|
|
SyncInteger(int initialValue,
int minValue,
int maxValue,
Object lock,
boolean useTimedOutException)
Creates an instance specifying all parameters. |
|
SyncInteger(int initialValue,
Object lock)
|
|
SyncInteger(int initialValue,
Object lock,
boolean useTimedOutException)
|
|
| Method Summary | |
int |
decreaseValueBy(int decrement)
Decrements the current value by the specified amount and returns the resulting new value. |
Object |
getLockObject()
Returns the reference to the object that is synchronized on by other methods in the implementing class. |
int |
getMaxValue()
|
int |
getMinValue()
|
int |
getValue()
|
int |
increaseValueBy(int increment)
Increments the current value by the specified amount and returns the resulting new value. |
boolean |
isMaxValue()
|
boolean |
isMinValue()
|
boolean |
isUseTimedOutExceptionSet()
Used to determine whether or not TimedOutException
will be thrown by methods when timout periods expire. |
boolean |
isZero()
|
void |
setMaxValue(int newMaxValue)
|
void |
setMinValue(int newMinValue)
|
void |
setUseTimedOutException(boolean useException)
Used to change the timeout behavior of methods on the implementing classes. |
void |
setValue(int newValue)
|
void |
setValueRange(int newMinValue,
int newMaxValue)
|
void |
shutdown()
A full shutdown is immediately initiated resulting in a ShutdownException being thrown by any methods
that modify the object or wait for it to be modified. |
String |
toString()
|
void |
waitForValueToChange()
|
boolean |
waitForValueToChange(long msTimeout)
|
void |
waitForValueToClimbTo(int atLeast)
|
boolean |
waitForValueToClimbTo(int atLeast,
long msTimeout)
|
void |
waitForValueToFallTo(int atMost)
|
boolean |
waitForValueToFallTo(int atMost,
long msTimeout)
|
void |
waitUntilMaxValue()
|
boolean |
waitUntilMaxValue(long msTimeout)
|
void |
waitUntilMinValue()
|
boolean |
waitUntilMinValue(long msTimeout)
|
void |
waitUntilValueInRange(int min,
int max)
|
boolean |
waitUntilValueInRange(int min,
int max,
long msTimeout)
|
void |
waitUntilValueIs(int val)
|
boolean |
waitUntilValueIs(int val,
long msTimeout)
|
void |
waitUntilZero()
|
boolean |
waitUntilZero(long msTimeout)
|
void |
waitWhileMaxValue()
|
boolean |
waitWhileMaxValue(long msTimeout)
|
void |
waitWhileMinValue()
|
boolean |
waitWhileMinValue(long msTimeout)
|
void |
waitWhileValueInRange(int min,
int max)
|
boolean |
waitWhileValueInRange(int min,
int max,
long msTimeout)
|
void |
waitWhileValueIs(int val)
|
boolean |
waitWhileValueIs(int val,
long msTimeout)
|
void |
waitWhileZero()
|
boolean |
waitWhileZero(long msTimeout)
|
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public SyncInteger(int initialValue,
int minValue,
int maxValue,
Object lock,
boolean useTimedOutException)
throws IllegalArgumentException
initialValue - the value to start off with.minValue - the lowest value permitted.maxValue - the highest value permitted.lock - an alternate object to synchronize on. If
null, synchronization is on this instance of
SyncInteger. See AccessibleLock.useTimedOutException - if true a TimedOutException will be thrown when method calls timeout.
If false, the timeout is indicated by the return
value. See TimedOutExceptionOption.
public SyncInteger(int initialValue,
int minValue,
int maxValue)
throws IllegalArgumentException
public SyncInteger(int initialValue,
Object lock,
boolean useTimedOutException)
public SyncInteger(int initialValue,
Object lock)
public SyncInteger(int initialValue)
public SyncInteger()
| Method Detail |
public Object getLockObject()
AccessibleLock
This reference can be used in synchronized blocks
to keep other threads from sneaking in between methods calls
like this:
|
obj = //...
synchronized ( obj.getLockObject() ) {
obj.methodA();
obj.methodB();
}
|
getLockObject in interface AccessibleLockcom.jthreadkit.AccessibleLock
public void setUseTimedOutException(boolean useException)
throws ShutdownException
TimedOutExceptionOptiontrue
methods will throw a TimedOutException when the
specified timeout period expires. If this option
is not set, methods usually signal a timeout through a
return value. Use
isUseTimedOutExceptionSet()
to check the current setting.setUseTimedOutException in interface TimedOutExceptionOptioncom.jthreadkit.TimedOutExceptionOptionuseException - if true is passed then
TimedOutException will be thrown after timeouts,
if false is passed, timeouts do not result
in exceptions.ShutdownException - if this object has already
been shutdown.public boolean isUseTimedOutExceptionSet()
TimedOutExceptionOptionTimedOutException
will be thrown by methods when timout periods expire.
The setUseTimedOutException()
method is used to change this setting.isUseTimedOutExceptionSet in interface TimedOutExceptionOptioncom.jthreadkit.TimedOutExceptionOptiontrue if timeouts will result in exceptions
being thrown, false otherwise.public void shutdown()
SimpleShutdownShutdownException being thrown by any methods
that modify the object or wait for it to be modified.
See this explanation for more.shutdown in interface SimpleShutdownpublic int getValue()
public void setValue(int newValue)
throws IllegalArgumentException,
ShutdownException
public int increaseValueBy(int increment)
throws IllegalArgumentException,
ShutdownException
public int decreaseValueBy(int decrement)
throws IllegalArgumentException,
ShutdownException
public void setMinValue(int newMinValue)
throws IllegalArgumentException,
ShutdownException
public int getMinValue()
public void setMaxValue(int newMaxValue)
throws IllegalArgumentException,
ShutdownException
public int getMaxValue()
public void setValueRange(int newMinValue,
int newMaxValue)
throws IllegalArgumentException,
ShutdownException
public boolean isMinValue()
public boolean isMaxValue()
public boolean isZero()
public boolean waitUntilValueInRange(int min,
int max,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException,
IllegalArgumentException
public void waitUntilValueInRange(int min,
int max)
throws InterruptedException,
ShutdownException,
IllegalArgumentException
public boolean waitWhileValueInRange(int min,
int max,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException,
IllegalArgumentException
public void waitWhileValueInRange(int min,
int max)
throws InterruptedException,
ShutdownException,
IllegalArgumentException
public boolean waitForValueToChange(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitForValueToChange()
throws InterruptedException,
ShutdownException
public boolean waitForValueToFallTo(int atMost,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitForValueToFallTo(int atMost)
throws InterruptedException,
ShutdownException
public boolean waitForValueToClimbTo(int atLeast,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitForValueToClimbTo(int atLeast)
throws InterruptedException,
ShutdownException
public boolean waitWhileZero(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitWhileZero()
throws InterruptedException,
ShutdownException
public boolean waitUntilZero(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitUntilZero()
throws InterruptedException,
ShutdownException
public boolean waitWhileValueIs(int val,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitWhileValueIs(int val)
throws InterruptedException,
ShutdownException
public boolean waitUntilValueIs(int val,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitUntilValueIs(int val)
throws InterruptedException,
ShutdownException
public boolean waitWhileMaxValue(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitWhileMaxValue()
throws InterruptedException,
ShutdownException
public boolean waitUntilMaxValue(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitUntilMaxValue()
throws InterruptedException,
ShutdownException
public boolean waitWhileMinValue(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitWhileMinValue()
throws InterruptedException,
ShutdownException
public boolean waitUntilMinValue(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
public void waitUntilMinValue()
throws InterruptedException,
ShutdownException
public String toString()
toString in class Object
|
JThreadKitTM v1.1.0 ( public members only)
|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||