📄 balking.html
字号:
<html><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html> <head><title>Balking</title></head><BODY bgcolor=#ffffee vlink=#0000aa link=#cc0000><h1>Balking</h1>Rather than implementing synchronization primitives, it is sometimespossible to eliminate suspensions entirely by changing the protocolsof the associated methods to place the burden on clients. Rather thantranslating guarded expressions into methods performing concurrencycontrol, you can revise the associated methods to ``balk''; i.e., toreturn <EM>failure</EM> indications to clients (for example booleanflags as return values; exceptions) that tell clients that the objectis not in a state that can handle the request. Clients will have totry again later or take other evasive action. <P>For example, applying this to the <ahref="synchDesign.html#secCounter" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/synchDesign.html#secCounter"> <CODE>BoundedCounter</CODE></a>class:<PRE>class CannotIncrement extends Exception { }class CannotDecrement extends Exception { }class BoundedCounterV7 { public synchronized int value() { return count_; } public synchronized void inc() { if (count_ >= maxVal) throw new CannotIncrement(); else ++count_; } public synchronized void dec() { if (count_ <= minVal) throw new CannotDecrement(); else --count_; } public BoundedCounterV7() { count_ = minValue; } private int count_; }</PRE>Note that this class does not <CODE>implement</CODE><CODE>BoundedCounter</CODE> as it was originally defined. <P>This style is a familiar and common one for people with experienceprogramming in purely sequential languages with exceptions. However,it is somewhat less common in strictly concurrent settings. Transformationinto balking form generally applies only when the availability ofevasive actions upon failure are intrinsic aspects of the design;normally for <EM>service</EM> actions in which clients are preparedfor service failures of various kinds. But whenever the client's onlyrecourse is to <EM>re-try</EM> the invocation, this transformationresults in similar concurrency control issues in the client, so theentire problem just gets pushed back one level. Sometimes, this iswhere responsibility for the issue belongs; other times not.<p>For example, when implementing a basic data structure like a<code>Stack</code>, balking on attempts to take an element from anempty stack is attractive since it makes the class usable in strictlysequential settings where it would be pointless to wait -- therearen't any other objects running in different threads that might addan element, thus resuming from a wait. Instead the program would juststall forever. On the other hand, choosing to balk means that clientsthat do wish to use the stack in such a multithreaded fashion willhave to build a layer on top of the balking version that instead causeit to wait. Since it is very difficult to build data structure classesthat support both styles at once (and because the resulting behaviorcan be chaotic), usually the balking version is chosen -- It is nottoo hard for clients to layer synchronized versions on top of it usinga <a href="coordinators.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/coordinators.html"> Coordinator</a>, but layering balkingon top of a synchronized version is sometimes impossible withoutchanging the implementation of the class itself.<p><a href="aopintro.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/aopintro.html">[Concurrent Programming in Java]</a><hr><address><A HREF="javascript:if(confirm('http://g.oswego.edu/dl \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://g.oswego.edu/dl'" tppabs="http://g.oswego.edu/dl">Doug Lea</A></address><!-- hhmts start -->Last modified: Tue Feb 20 06:28:59 EST 1996<!-- hhmts end --></body> </html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -