Guarded suspension
   HOME

TheInfoList



OR:

In
concurrent programming Concurrent means happening at the same time. Concurrency, concurrent, or concurrence may refer to: Law * Concurrence, in jurisprudence, the need to prove both ''actus reus'' and ''mens rea'' * Concurring opinion (also called a "concurrence"), ...
, guarded suspension is a
software design pattern In software engineering, a software design pattern is a general, reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine co ...
for managing operations that require both a
lock Lock(s) may refer to: Common meanings *Lock and key, a mechanical device used to secure items of importance *Lock (water navigation), a device for boats to transit between different levels of water, as in a canal Arts and entertainment * ''Lock ...
to be acquired and a
precondition In computer programming, a precondition is a condition or predicate that must always be true just prior to the execution of some section of code or before an operation in a formal specification. If a precondition is violated, the effect of the s ...
to be satisfied before the operation can be executed. The guarded suspension pattern is typically applied to method calls in object-oriented programs, and involves suspending the method call, and the calling thread, until the precondition (acting as a
guard Guard or guards may refer to: Professional occupations * Bodyguard, who protects an individual from personal assault * Crossing guard, who stops traffic so pedestrians can cross the street * Lifeguard, who rescues people from drowning * Prison ...
) is satisfied.


Usage

Because it is blocking, the guarded suspension pattern is generally only used when the developer knows that a method call will be suspended for a finite and reasonable period of time. If a method call is suspended for too long, then the overall program will slow down or stop, waiting for the precondition to be satisfied. If the developer knows that the method call suspension will be indefinite or for an unacceptably long period, then the balking pattern may be preferred.


Implementation

In Java, the Object class provides the wait() and notify() methods to assist with guarded suspension. In the implementation below, originally found in , if there is no precondition satisfied for the method call to be successful, then the method will wait until it finally enters a valid state. public class Example An example of an actual implementation would be a queue object with a get method that has a guard to detect when there are no items in the queue. Once the put method notifies the other methods (for example, a get method), then the get method can exit its guarded state and proceed with a call. Once the queue is empty, then the get method will enter a guarded state once again.


See also

* Balking pattern is an alternative pattern for dealing with a precondition * Guarded Command Language includes a similar language construct *
Readers–writer lock In computer science, a readers–writer (single-writer lock, a multi-reader lock, a push lock, or an MRSW lock) is a Synchronization (computer science), synchronization primitive that solves one of the readers–writers problems. An RW lock allo ...


Notes


References

*. Software design patterns {{compu-prog-stub