Source Code Cross Referenced for WaitQueue.java in  » Library » Apache-beehive-1.0.2-src » org » apache » beehive » netui » util » internal » 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 » Library » Apache beehive 1.0.2 src » org.apache.beehive.netui.util.internal.concurrent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:          based on file: QueuedSemaphore.java
003:          Originally written by Doug Lea and released into the public domain.
004:          This may be used for any purposes whatsoever without acknowledgment.
005:          Thanks for the assistance and support of Sun Microsystems Labs,
006:          and everyone contributing, testing, and using this code.
007:          History:
008:          Date       Who                What
009:          11Jun1998  dl               Create public version
010:           5Aug1998  dl               replaced int counters with longs
011:          24Aug1999  dl               release(n): screen arguments
012:         */
013:
014:        package org.apache.beehive.netui.util.internal.concurrent;
015:
016:        import java.util.*;
017:
018:        /**
019:         * Base class for internal queue classes for semaphores, etc.
020:         * Relies on subclasses to actually implement queue mechanics.
021:         * NOTE: this class is NOT present in java.util.concurrent.
022:         **/
023:
024:        abstract class WaitQueue {
025:
026:            public abstract void insert(WaitNode w); // assumed not to block
027:
028:            public abstract WaitNode extract(); // should return null if empty
029:
030:            public abstract boolean hasNodes();
031:
032:            public abstract int getLength();
033:
034:            public abstract Collection getWaitingThreads();
035:
036:            public abstract boolean isWaiting(Thread thread);
037:
038:            static interface QueuedSync {
039:                // invoked with sync on wait node, (atomically) just before enqueuing
040:                boolean recheck(WaitNode node);
041:
042:                // invoked with sync on wait node, (atomically) just before signalling
043:                void takeOver(WaitNode node);
044:            }
045:
046:            static class WaitNode {
047:                boolean waiting = true;
048:                WaitNode next = null;
049:                final Thread owner;
050:
051:                public WaitNode() {
052:                    this .owner = Thread.currentThread();
053:                }
054:
055:                public Thread getOwner() {
056:                    return owner;
057:                }
058:
059:                public synchronized boolean signal(QueuedSync sync) {
060:                    boolean signalled = waiting;
061:                    if (signalled) {
062:                        waiting = false;
063:                        notify();
064:                        sync.takeOver(this );
065:                    }
066:                    return signalled;
067:                }
068:
069:                public synchronized boolean doTimedWait(QueuedSync sync,
070:                        long nanos) throws InterruptedException {
071:                    if (sync.recheck(this ) || !waiting)
072:                        return true;
073:                    else if (nanos <= 0) {
074:                        waiting = false;
075:                        return false;
076:                    } else {
077:                        long deadline = Utils.nanoTime() + nanos;
078:                        try {
079:                            for (;;) {
080:                                TimeUnit.NANOSECONDS.timedWait(this , nanos);
081:                                if (!waiting) // definitely signalled
082:                                    return true;
083:                                else {
084:                                    nanos = deadline - Utils.nanoTime();
085:                                    if (nanos <= 0) { //  timed out
086:                                        waiting = false;
087:                                        return false;
088:                                    }
089:                                }
090:                            }
091:                        } catch (InterruptedException ex) {
092:                            if (waiting) { // no notification
093:                                waiting = false; // invalidate for the signaller
094:                                throw ex;
095:                            } else { // thread was interrupted after it was notified
096:                                Thread.currentThread().interrupt();
097:                                return true;
098:                            }
099:                        }
100:                    }
101:                }
102:
103:                public synchronized void doWait(QueuedSync sync)
104:                        throws InterruptedException {
105:                    if (!sync.recheck(this )) {
106:                        try {
107:                            while (waiting)
108:                                wait();
109:                        } catch (InterruptedException ex) {
110:                            if (waiting) { // no notification
111:                                waiting = false; // invalidate for the signaller
112:                                throw ex;
113:                            } else { // thread was interrupted after it was notified
114:                                Thread.currentThread().interrupt();
115:                                return;
116:                            }
117:                        }
118:                    }
119:                }
120:            }
121:
122:        }
w_w_w_.___j__a_v_a2__s__.___c___om_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.