Source Code Cross Referenced for Latch.java in  » Ajax » Laszlo-4.0.10 » EDU » oswego » cs » dl » util » concurrent » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » Laszlo 4.0.10 » EDU.oswego.cs.dl.util.concurrent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:          File: Latch.java
003:
004:          Originally written by Doug Lea and released into the public domain.
005:          This may be used for any purposes whatsoever without acknowledgment.
006:          Thanks for the assistance and support of Sun Microsystems Labs,
007:          and everyone contributing, testing, and using this code.
008:
009:          History:
010:          Date       Who                What
011:          11Jun1998  dl               Create public version
012:         */
013:
014:        package EDU.oswego.cs.dl.util.concurrent;
015:
016:        /**
017:         * A latch is a boolean condition that is set at most once, ever.
018:         * Once a single release is issued, all acquires will pass.
019:         * <p>
020:         * <b>Sample usage.</b> Here are a set of classes that use
021:         * a latch as a start signal for a group of worker threads that
022:         * are created and started beforehand, and then later enabled.
023:         * <pre>
024:         * class Worker implements Runnable {
025:         *   private final Latch startSignal;
026:         *   Worker(Latch l) { startSignal = l; }
027:         *    public void run() {
028:         *      startSignal.acquire();
029:         *      doWork();
030:         *   }
031:         *   void doWork() { ... }
032:         * }
033:         *
034:         * class Driver { // ...
035:         *   void main() {
036:         *     Latch go = new Latch();
037:         *     for (int i = 0; i < N; ++i) // make threads
038:         *       new Thread(new Worker(go)).start();
039:         *     doSomethingElse();         // don't let run yet 
040:         *     go.release();              // let all threads proceed
041:         *   } 
042:         * }
043:         *</pre>
044:         * [<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p>
045:         **/
046:
047:        public class Latch implements  Sync {
048:            protected boolean latched_ = false;
049:
050:            /*
051:              This could use double-check, but doesn't.
052:              If the latch is being used as an indicator of
053:              the presence or state of an object, the user would
054:              not necessarily get the memory barrier that comes with synch
055:              that would be needed to correctly use that object. This
056:              would lead to errors that users would be very hard to track down. So, to
057:              be conservative, we always use synch.
058:             */
059:
060:            public void acquire() throws InterruptedException {
061:                if (Thread.interrupted())
062:                    throw new InterruptedException();
063:                synchronized (this ) {
064:                    while (!latched_)
065:                        wait();
066:                }
067:            }
068:
069:            public boolean attempt(long msecs) throws InterruptedException {
070:                if (Thread.interrupted())
071:                    throw new InterruptedException();
072:                synchronized (this ) {
073:                    if (latched_)
074:                        return true;
075:                    else if (msecs <= 0)
076:                        return false;
077:                    else {
078:                        long waitTime = msecs;
079:                        long start = System.currentTimeMillis();
080:                        for (;;) {
081:                            wait(waitTime);
082:                            if (latched_)
083:                                return true;
084:                            else {
085:                                waitTime = msecs
086:                                        - (System.currentTimeMillis() - start);
087:                                if (waitTime <= 0)
088:                                    return false;
089:                            }
090:                        }
091:                    }
092:                }
093:            }
094:
095:            /** Enable all current and future acquires to pass **/
096:            public synchronized void release() {
097:                latched_ = true;
098:                notifyAll();
099:            }
100:
101:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.