In concurrent programming, guarded suspension[1] is a software design pattern for managing operations that require both a lock to be acquired and a precondition 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) is satisfied.

Usage

edit

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

edit

In Java, the Object class provides the wait() and notify() methods to assist with guarded suspension. In the implementation below, originally found in Kuchana (2004), 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 {
    synchronized void guardedMethod() {
        while (!preCondition()) {
            try {
                // Continue to wait
                wait();
                // …
            } catch (InterruptedException e) {
                // …
            }
        }
        // Actual task implementation
    }
    synchronized void alterObjectStateMethod() {
        // Change the object state
        // …
        // Inform waiting threads
        notify();
    }
}

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

edit

Notes

edit
  1. ^ Lea, Doug (2000). Concurrent Programming in Java Second Edition. Reading, MA: Addison-Wesley. ISBN 0-201-31009-0.

References

edit
  • Kuchana, Partha (2004). "Software Architecture Design Patterns in Java" (Document). Boca Raton, Florida: Auerbach Publications..


📚 Artikel Terkait di Wikipedia

Balking pattern

time, then the guarded suspension pattern may be preferred. Below is a general, simple example for an implementation of the balking pattern. As demonstrated

Concurrency pattern

Double-checked locking Guarded suspension Monitor object Nuclear reaction Reactor pattern Readers–writer lock Scheduler pattern Thread pool pattern Thread-local

Suspension

suspension Cell suspension or suspension culture, in biology Guarded suspension, a software design pattern in concurrent programming suspending a method call and

Software design pattern

A software design pattern describes a reusable solution to a commonly needed behavior in software. A design pattern is not a rigid structure to be copied

Guard (computer science)

in Guarded Command Language, a language in formal methods. Guards can be used to augment pattern matching with the possibility to skip a pattern even

Gewehr 98

of the rear sight base. This was done since the 1888 pattern M/88 cartridge and 1903 S-bore pattern cartridge are two different non-interchangeable chamberings

Hermès

Tompkins of U.S. News & World Report called the company "one of Paris' best guarded jewels." In the following years, Dumas decreased Hermès' franchises from

Super Bowl XXXVIII halftime show controversy

Speaking to Robert Tannenbaum of Blender Magazine, Jackson had strong, if guarded, views on the reactions, reiterating her embarrassment at being exposed