|
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.SyncBoolean
SyncBoolean is used to signal true and
false states among two or more threads.
SyncBoolean encapsulates Java's wait-notify mechanism making
it simple and safe for one thread to wait for a certain state to be set
by another thread. In fact, any number of threads can be waiting
and any number of threads can safely change the state.
For example, threadA
waits until the value is true. Meanwhile sometime soon
threadB comes along and sets the value to be
true, then threadA proceeds.
Specifically, if there's an instance of SyncBoolean
constructed:
|
SyncBoolean sb = new |
threadA executes this code:
|
try {
sb. |
threadB executes this code:
|
sb. |
threadA returns from waitUntilTrue()
and continues on. If threadA calls
waitUntilTrue() and the value is already true,
it returns right away with no delay.
SyncBoolean can also be used as a mutex to control
concurrent access to a block of code instead of using
synchronized. The advantage is that threads waiting
to get into the protected block can use a timeout to limit their
wait and/or they respond to an interrupt by throwing an
InterruptedException (threads blocked waiting to get into
a synchronized block do not respond to interrupts - see
Java Thread Programming
for more on interrupts and synchronization). This can be done
with an instance of SyncBoolean referenced
by sb:
|
// When false, the lock is free to use, wait for it... sb. |
sb must be false and
that when any thread is in the try block, sb
is true.
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 | |
SyncBoolean()
Builds an instance with an initial value of false. |
|
SyncBoolean(boolean initialValue)
Builds an instance with the specified initial value. |
|
SyncBoolean(boolean initialValue,
Object lock,
boolean useTimedOutException)
Builds an instance with the specified initial value. |
|
| Method Summary | |
Object |
getLockObject()
Returns the reference to the object that is synchronized on by other methods in the implementing class. |
boolean |
ifFalseSetTrue()
If the value is currently false, it is changed to
be true. |
boolean |
ifTrueSetFalse()
If the value is currently true, it is changed to
be false. |
boolean |
isFalse()
Tests to see if the value is currently false. |
boolean |
isTrue()
Tests to see if the value is currently true. |
boolean |
isUseTimedOutExceptionSet()
Used to determine whether or not TimedOutException
will be thrown by methods when timout periods expire. |
void |
setUseTimedOutException(boolean useException)
Used to change the timeout behavior of methods on the implementing classes. |
void |
setValue(boolean newValue)
Sets the value to the specified new value. |
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. |
void |
toggleValue()
Toggles the value from true to false
of from false to true. |
String |
toString()
Shows the classname and current value. |
void |
waitForValueToChange()
Waits for the value to change from its current value. |
boolean |
waitForValueToChange(long msTimeout)
Waits for the value to change from its current value. |
void |
waitToSetFalse()
Waits for the value to become true and then
sets it to be false. |
boolean |
waitToSetFalse(long msTimeout)
Waits for the value to become true and then
sets it to be false. |
void |
waitToSetTrue()
Waits for the value to become false and then
sets it to be true. |
boolean |
waitToSetTrue(long msTimeout)
Waits for the value to become false and then
sets it to be true. |
void |
waitUntilFalse()
Waits for the value to become false. |
boolean |
waitUntilFalse(long msTimeout)
Waits for the value to become false. |
void |
waitUntilTrue()
Waits for the value to become true. |
boolean |
waitUntilTrue(long msTimeout)
Waits for the value to become true. |
void |
waitUntilValueIs(boolean targetValue)
Waits for the value to match the specified target value. |
boolean |
waitUntilValueIs(boolean targetValue,
long msTimeout)
Waits for the value to match the specified target value. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Constructor Detail |
public SyncBoolean(boolean initialValue,
Object lock,
boolean useTimedOutException)
lock object is synchronized on
internally to control concurrent access (see getLockObject()).
The useTimedOutException flag indicates whether or
not a TimedOutException will be thrown when methods timeout
(see explanation).initialValue - the value to start with.lock - the object to synchronize on. If null
is passed, this is used.useTimedOutException - if true,
TimedOutException will be thrown from methods
when they timeout. If false, timeouts are indicated
by return value.public SyncBoolean(boolean initialValue)
this and timeouts
will not throw exceptions. Internally, the
main constructor
is called like this:
|
this(initialValue, null, false); |
public SyncBoolean()
false.
Synchronization will be on this and timeouts
will not throw exceptions. Internally, the
main constructor
is called like this:
|
this(false, null, false); |
| 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 SimpleShutdown
public void setValue(boolean newValue)
throws ShutdownException
newValue - the value to change to.ShutdownException - see explanation.public boolean isTrue()
true.true if the value is true,
false if the value is false.public boolean isFalse()
false.true if the value is false,
false if the value is true.
public void toggleValue()
throws ShutdownException
true to false
of from false to true.ShutdownException - see explanation.
public boolean ifFalseSetTrue()
throws ShutdownException
false, it is changed to
be true. Either way, when this method call is complete,
the value is true.true if the value was changed,
false otherwise.ShutdownException - see explanation.
public boolean ifTrueSetFalse()
throws ShutdownException
true, it is changed to
be false. Either way, when this method call is complete,
the value is false.true if the value was changed,
false otherwise.ShutdownException - see explanation.
public boolean waitToSetTrue(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
false and then
sets it to be true.msTimeout - maximum number of milliseconds to wait. Use
ThreadTools.NO_TIMEOUT (or 0) to indicate that
the waiting should never timeout.true if successful, false if
a timeout occurs (and the timeout option is
not set to throw exceptions
[see explanation]).InterruptedException - see explanation.TimedOutException - see explanation.ShutdownException - see explanation.
public void waitToSetTrue()
throws InterruptedException,
ShutdownException
false and then
sets it to be true. Equivalent to:
|
SyncBoolean sb = //... synchronized ( sb. |
InterruptedException - see explanation.ShutdownException - see explanation.
public boolean waitToSetFalse(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
true and then
sets it to be false.msTimeout - maximum number of milliseconds to wait. Use
ThreadTools.NO_TIMEOUT (or 0) to indicate that
the waiting should never timeout.true if successful, false if
a timeout occurs (and the timeout option is
not set to throw exceptions
[see explanation]).InterruptedException - see explanation.TimedOutException - see explanation.ShutdownException - see explanation.
public void waitToSetFalse()
throws InterruptedException,
ShutdownException
true and then
sets it to be false.InterruptedException - see explanation.ShutdownException - see explanation.
public boolean waitUntilTrue(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
true.msTimeout - maximum number of milliseconds to wait. Use
ThreadTools.NO_TIMEOUT (or 0) to indicate that
the waiting should never timeout.true if successful, false if
a timeout occurs (and the timeout option is
not set to throw exceptions
[see explanation]).InterruptedException - see explanation.TimedOutException - see explanation.ShutdownException - see explanation.
public void waitUntilTrue()
throws InterruptedException,
ShutdownException
true.InterruptedException - see explanation.ShutdownException - see explanation.
public boolean waitUntilFalse(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
false.msTimeout - maximum number of milliseconds to wait. Use
ThreadTools.NO_TIMEOUT (or 0) to indicate that
the waiting should never timeout.true if successful, false if
a timeout occurs (and the timeout option is
not set to throw exceptions
[see explanation]).InterruptedException - see explanation.TimedOutException - see explanation.ShutdownException - see explanation.
public void waitUntilFalse()
throws InterruptedException,
ShutdownException
false.InterruptedException - see explanation.ShutdownException - see explanation.
public boolean waitForValueToChange(long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
msTimeout - maximum number of milliseconds to wait. Use
ThreadTools.NO_TIMEOUT (or 0) to indicate that
the waiting should never timeout.true if successful, false if
a timeout occurs (and the timeout option is
not set to throw exceptions
[see explanation]).InterruptedException - see explanation.TimedOutException - see explanation.ShutdownException - see explanation.
public void waitForValueToChange()
throws InterruptedException,
ShutdownException
InterruptedException - see explanation.ShutdownException - see explanation.
public boolean waitUntilValueIs(boolean targetValue,
long msTimeout)
throws InterruptedException,
TimedOutException,
ShutdownException
targetValue - the value to wait for.msTimeout - maximum number of milliseconds to wait. Use
ThreadTools.NO_TIMEOUT (or 0) to indicate that
the waiting should never timeout.true if successful, false if
a timeout occurs (and the timeout option is
not set to throw exceptions
[see explanation]).InterruptedException - see explanation.TimedOutException - see explanation.ShutdownException - see explanation.
public void waitUntilValueIs(boolean targetValue)
throws InterruptedException,
ShutdownException
targetValue - the value to wait for.InterruptedException - see explanation.ShutdownException - see explanation.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 | ||||||||