⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 coordinators.html

📁 Concurrent Programming in Java
💻 HTML
字号:
<html><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html> <head><title>Coordinator Objects</title></head><BODY bgcolor=#ffffee vlink=#0000aa link=#cc0000><h1>Coordinator Objects</h1>Coordinator designs separate concurrency control from base funcationality.<p>Bare, unsynchronized `` ground'' objects may be exclusively managed bycoordinator objects. A coordinator normally just forwards messages onto the ground object, but may postpone doing so when the ground objectis not in an appproriate state. Instances of this style of coordinatorhave exactly the same interface as the ground objects, plus possiblyadditional contol methods to change delegation targets and strategies.<P>Coordinators serve as lighter-weight delegation-based versions of <ahref="acceptor.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/acceptor.html">MetaObjects</a>,   They have many of the same advantagesin helping to separate concurrency control from ground actions.  Butrather than interpreting message packets, they act upon regular Javamessages by sending the same kind of message but to the delegate.<p>The Coordinator pattern is most applicable when:<ul>  <li> You need an Adaptor (See the <A      HREF="javascript:if(confirm('http://st-www.cs.uiuc.edu/users/patterns/Books.html  \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://st-www.cs.uiuc.edu/users/patterns/Books.html'" tppabs="http://st-www.cs.uiuc.edu/users/patterns/Books.html"> Design      Patterns</A> book or <a      href="javascript:if(confirm('http://g.oswego.edu/dl/oosdw3/ch16/ch16.html  \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/oosdw3/ch16/ch16.html'" tppabs="http://g.oswego.edu/dl/oosdw3/ch16/ch16.html"> OOSD</a>.)      for a class that was designed originally to work only in      purely sequential settings.  <li> You would like to adapt a class using failure-based       <a href="balking.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/balking.html">balking</a> protocols to instead        use wait-based ones.  <li> You would like to layer different kinds of concurrency control       over the same base functionality.  <li>  Conversely, you would like to support       a particular style of control that can be used across different       ground object classes. (This is a kind of       <A HREF="javascript:if(confirm('http://www.parc.xerox.com/PARC/spl/eca/oi.html  \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://www.parc.xerox.com/PARC/spl/eca/oi.html'" tppabs="http://www.parc.xerox.com/PARC/spl/eca/oi.html">Open       Implementation</A> design).</ul><p> For an example of the first, most common case, we can design acoordinator-based version of <a href="synchDesign.html#secCounter" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/synchDesign.html#secCounter">BoundedCounters</a> starting with a bare, unprotected groundobject type:<PRE>class GroundCounter {   public int value()     { return count_; }  public void inc()      { ++count_; }  public void dec()      { --count_; }   public GroundCounter() { count_ = BoundedCounter.minValue; }  private int count_;}</PRE>Note that even though it supports the methods declared in<code>BoundedCounter, </code><CODE>GroundCounter</CODE> is notdeclared to <CODE>implement</CODE> <CODE>BoundedCounter</CODE>, sinceit does not maintain the invariants listed in the<CODE>BoundedCounter</CODE> interface. In fact, its methods are noteven declared as <code>synchronized</code>, so it would be a bad ideato use it at all in a multithreaded Java program.  You'd probably notwant to list the class itself as <CODE>public</CODE> in the package itis defined in.<P><p>Ground objects can only be used to the righteffect when <em>exclusively</em> managed by coordinators that:<ul>  <li> Grab the Java object lock for the ground object whenever       accessing it in any way. This is done using Java per-block       <code>synchronized</code> syntax. (See <a href="latches.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/latches.html">       latches</a> and <a href="trans.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/trans.html">transactions</a> for other       usages and variants of Java block-synchronization.)  <li> Implement the appropriate waiting and notification on       behalf of the ground object.</ul><p> More generally, depending on the nature of the ground class, thecoordinator might have either or both of these features (That is, forexample, only the second if the ground object already has synchronizedmethods but needs other control.)<p>A coordinator for <code>BoundedCounter</code> can be defined as:<PRE>class BoundedCounterV4 implements BoundedCounter {  public synchronized int value()  {    synchronized (del_) { return del_.value(); }  }  public synchronized void inc()   {    waitUntilIncrementable();    synchronized (del_) { del_.inc(); }    notifyAll();  }  public synchronized void dec()   {    waitUntilDecrementable();    synchronized (del_) { del_.dec(); }    notifyAll();  }   public BoundedCounterV4()        { del_ = new GroundCounter(); }  private GroundCounter del_;  private synchronized void waitUntilIncrementable() {    for (;;) {      synchronized(del_) { if (del_.value() &lt; maxVal) break; }      try { wait(); } catch(InterruptedException ex) {};    }  }  private synchronized void waitUntilDecrementable() {    for (;;) {      synchronized(del_) { if (del_.value() &gt; minVal) break; }      try { wait(); } catch(InterruptedException ex) {};    }  }}</PRE><H3><A NAME="secSelfDeleg"></A>Self-Delegation</H3>Often, you can rework Coordinator designs to use only one class, nottwo.  Using self-delegation, the same object controls its own actionsvia delegation to its own methods. To do this, separate out the codethat actually performs actions into non-public methods. The original<CODE>public</CODE> methods normally just directly invoke thenon-public ones, but may postpone doing so if necessary. <P>One way to define self-delegating implementations is to start with aCoordinator version, and then simply merge (relabeling as necessary)<CODE>public</CODE> declarations in the ground class as<CODE>private</CODE> in the merged class, changing calls to thedelegate to <CODE>this</CODE> calls, and then simplifying accordingly. Forexample, transforming the first version in this way leads to:<PRE>class BoundedCounterV6 implements BoundedCounter {  public synchronized int value()  { return count_; }  public synchronized void inc()   { waitUntilIncrementable(); this.inc_(); }  public synchronized void dec()   { waitUntilDecrementable(); this.dec(); }  public BoundedCounterV6()        { count_ = minValue; }  private synchronized void waitUntilIncrementable() {    while (this.value() &gt;= maxVal)      try { wait(); } catch(InterruptedException ex) {};  }  private synchronized void waitUntilDecrementable() {    while (this.value() &lt;= minVal)      try { wait(); } catch(InterruptedException ex) {};  }  private synchronized void inc_() { ++count_; this.notifyAll(); }  private synchronized void dec_() { --count_; this.notifyAll(); }   private int count_;}</PRE><h2>Adding notifications from ground objects</h2>Some ground classes resist this kind of simple layering.  Coordinatorscan only sense and signal conditions obtainable via<CODE>public</CODE> methods in the delegates. Sometimes it is notdesirable or even possible to make such information<CODE>public</CODE> in a way that can be used for timely concurrencycontrol. In this case, you must re-design the ground class tosupply <a href="delegnotif.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/delegnotif.html">subject/observer-stylenotifications</a>, in which the delegate plays the role of subject,notifying the coordinator upon changes. For an example in which theground class also declares its methods as <code>synchronized</code>so the coordinator does not need to perform per-block synchronization:<PRE>class NotifyingCounter {  public synchronized int value()  { return count_; }  public synchronized void inc()   { ++count_; ob_.notifyAll(); }  public synchronized void dec()   { --count_; ob_.notifyAll(); }   NotifyingCounter(Object ob)      { ob_ = ob;                                      count_ = BoundedCounter.minValue; }  private int count_;  private Object ob_;}class BoundedCounterV5 implements BoundedCounter {  public synchronized int value()  { return del_.value(); }  public synchronized void inc()   { waitUntilIncrementable(); del_.inc(); }  public synchronized void dec()   { waitUntilDecrementable(); del_.dec(); }  public BoundedCounterV5()        { del_ =new NotifyingCounter();}  private NotifyingCounter del_;  private synchronized void waitUntilIncrementable() {    while (del_.value() &gt;= maxVal)      try { wait(); } catch(InterruptedException ex) {};  }  private synchronized void waitUntilDecrementable() {    while (del_.value() &lt;= minVal)      try { wait(); } catch(InterruptedException ex) {};  }}</PRE><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 + -