Origin: |
Lea 97 |
Reason: |
Disregard the call to a method of an object, if the state
of the object is not appropriate to execute the call.
|
Synopsis: |
Test the object's state, in a synchronized block
for thread safety, and return if the state is inappropriate.
|
Example: |
A toilet with an automatic flusher should not begin a flush
cycle when another cycle is in progress.
|
Solution: |
In its simplest form, the potentially balking method looks like:
void balkingMethod () {
synchronized (this) {
if (! some_condition) return;
else
... // code possibly changing some_condition
}
... // do whatever
}
|
The synchronized block ensures thread safety.
|
See also: |
Guarded Suspension
(is similar to balkingMethod,
but it waits rather than returning)
|