?? latches.html
字號:
<html><!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"><html> <head><title>Latches</title></head><BODY bgcolor=#ffffee vlink=#0000aa link=#cc0000><h1>Latches</h1>A <em>latch</em> (sometimes called a <em>permanent</em>) is acondition or variable that changes its value at most once, ever.Examples include:<ul> <li> Events. A particular instance of an event has either not yet occurred, or has occurred. Once it occurs, it never un-occurs. (See, for example <A HREF="javascript:if(confirm('http://g.oswego.edu/dl/psl/psl/psl.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/psl/psl/psl.html'" tppabs="http://g.oswego.edu/dl/psl/psl/psl.html"> PSL </A> for ways of representing such occurrences more formally.) <li> Timing thresholds. Once it is past a certain point in time, the time never un-elapses. <li> Instance variables holding references to other objects, initialized to null in a constructor, but ultimately bound (assigned) exactly once later. <li> Instance variables representing that an object is in a special state from which it can never leave. For example, an end-of-file state of a non-rewindable file.</ul><p> The use of latches often simplifies concurrent object design andthe associated synchronization code. One of the main reasons forusing latches is to help structure <a href="delegnotif.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/delegnotif.html">delegated and mutual notifications</a>. Additionally, there are someminor implementation advantages in Java, including:<ul> <li> To wait for a time-latch, you can of course use <code>sleep</code> rather than hand-crafted <code>wait()</code> loops that recheck the time. Unless broken by an <code>InterruptedException</code>, you can be sure that the specified time has elapsed. <li> Since latches cannot change to one value and then change back, it is sometimes possible to replace the standard <a href="synchDesign.html" tppabs="http://www.foi.hr/~dpavlin/java/mirrors/g.oswego.edu/dl/pats/synchDesign.html"> guard idiom</a>: <br> <pre> while (!cond) try { wait(); } catch ... </pre> with: <br> <pre> if (!cond) try { wait(); } catch ... </pre> <p> However, this is still poor style, and not worth the savings except perhaps in special cases since it makes it almost impossible to use waits and notifications for other purposes in the associated class or its subclasses. <li> It is not always necessary to use fully <code>synchronized</code> methods when checking latches. This can be a minor performance enhancement and can help ensure liveness. If a latch variable (of a single primitive data type like ints and object references) has been set, there is no need to lock the object when accessing it, although if it hasn't, you still need to (since it may be in the process of being set). This can be expressed in Java using the Java block-synchronization syntax applied to the case where only part of a method need be synchronized.</ul>For example, suppose the constructor for a class <code>Server</code>created an initialization thread that established a<code>helper_</code>, which once set, was never changed:<pre>class Server { private Helper helper_; public Server() { helper_ = null; (new Thread(new HelperInitializer(this))).start(); ... } public void delegatedAction() { if (helper_ == null) { synchronized(this) { while (helper_ == null) try { wait(); } catch(InterruptedException ex) {} } } helper_help(); } public void initializeHelper(Helper h) { // callback from initializer helper_ = h; notifyAll(); }}</pre><blockquote> Footnote: The use of latches is essentially the same ideaas that of ``single static assignment form''for variables seen in somelanguages and their compilers. Such code is about equally awkward towrite, but provides about equal simplifications in other code anddesigns that rely upon them. </blockquote><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 + -