Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Guarded suspension
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{More citations needed|date=December 2010}} {{Use dmy dates|date=December 2019}} In [[concurrent computing|concurrent programming]], '''guarded suspension'''<ref>{{cite book |last=Lea |first=Doug |author-link=Doug Lea |title=Concurrent Programming in Java Second Edition |publisher=Addison-Wesley |location=Reading, MA |year=2000 |isbn=0-201-31009-0 |url-access=registration |url=https://archive.org/details/concurrentprogra00lead_0 }}</ref> is a [[design pattern (computer science)|software design pattern]] for managing operations that require both a [[lock (computer science)|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 (computer science)|guard]]) is satisfied. ==Usage== Because it is [[Blocking (computing)|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 <code>wait()</code> and <code>notify()</code> methods to assist with guarded suspension. In the implementation below, originally found in {{harvtxt|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. <syntaxhighlight lang="java"> 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(); } } </syntaxhighlight> An example of an actual implementation would be a queue object with a <code>get</code> method that has a guard to detect when there are no items in the queue. Once the <code>put</code> method notifies the other methods (for example, a <code>get</code> method), then the <code>get</code> method can exit its guarded state and proceed with a call. Once the queue is empty, then the <code>get</code> 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]] ==Notes== {{Reflist}} ==References== *{{Cite document | last = Kuchana | first = Partha | title = Software Architecture Design Patterns in Java | year = 2004 | publisher = Auerbach Publications | location = Boca Raton, Florida }}. {{Design Patterns patterns}} [[Category:Software design patterns]] {{compu-prog-stub}}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Cite book
(
edit
)
Template:Cite document
(
edit
)
Template:Compu-prog-stub
(
edit
)
Template:Design Patterns patterns
(
edit
)
Template:Harvtxt
(
edit
)
Template:More citations needed
(
edit
)
Template:Reflist
(
edit
)
Template:Use dmy dates
(
edit
)
Search
Search
Editing
Guarded suspension
Add topic