Source Code Cross Referenced for BoundedBuffer.java in  » Database-DBMS » Quadcap-Embeddable-Database » javax » 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 » Database DBMS » Quadcap Embeddable Database » javax.concurrent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:          File: BoundedBuffer.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:          17Jul1998  dl               Simplified by eliminating wait counts
013:          25aug1998  dl               added peek
014:           5May1999  dl               replace % with conditional (slightly faster)
015:         */
016:
017:        package javax.concurrent;
018:
019:        /**
020:         * Efficient array-based bounded buffer class.
021:         * Adapted from CPJ, chapter 8, which describes design.
022:         * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p>
023:         **/
024:
025:        public class BoundedBuffer implements  BoundedChannel {
026:
027:            protected final Object[] array_; // the elements
028:
029:            protected int takePtr_ = 0; // circular indices
030:            protected int putPtr_ = 0;
031:
032:            protected int usedSlots_ = 0; // length
033:            protected int emptySlots_; // capacity - length
034:
035:            /**
036:             * Helper monitor to handle puts. 
037:             **/
038:            protected final Object putMonitor_ = new Object();
039:
040:            /**
041:             * Create a BoundedBuffer with the given capacity.
042:             * @exception IllegalArgumentException if capacity less or equal to zero
043:             **/
044:            public BoundedBuffer(int capacity) throws IllegalArgumentException {
045:                if (capacity <= 0)
046:                    throw new IllegalArgumentException();
047:                array_ = new Object[capacity];
048:                emptySlots_ = capacity;
049:            }
050:
051:            /**
052:             * Create a buffer with the current default capacity
053:             **/
054:
055:            public BoundedBuffer() {
056:                this (DefaultChannelCapacity.get());
057:            }
058:
059:            /** 
060:             * Return the number of elements in the buffer.
061:             * This is only a snapshot value, that may change
062:             * immediately after returning.
063:             **/
064:            public synchronized int size() {
065:                return usedSlots_;
066:            }
067:
068:            public int capacity() {
069:                return array_.length;
070:            }
071:
072:            protected void incEmptySlots() {
073:                synchronized (putMonitor_) {
074:                    ++emptySlots_;
075:                    putMonitor_.notify();
076:                }
077:            }
078:
079:            protected synchronized void incUsedSlots() {
080:                ++usedSlots_;
081:                notify();
082:            }
083:
084:            protected final void insert(Object x) { // mechanics of put
085:                --emptySlots_;
086:                array_[putPtr_] = x;
087:                if (++putPtr_ >= array_.length)
088:                    putPtr_ = 0;
089:            }
090:
091:            protected final Object extract() { // mechanics of take
092:                --usedSlots_;
093:                Object old = array_[takePtr_];
094:                array_[takePtr_] = null;
095:                if (++takePtr_ >= array_.length)
096:                    takePtr_ = 0;
097:                return old;
098:            }
099:
100:            public Object peek() {
101:                synchronized (this ) {
102:                    if (usedSlots_ > 0)
103:                        return array_[takePtr_];
104:                    else
105:                        return null;
106:                }
107:            }
108:
109:            public void put(Object x) throws InterruptedException {
110:                if (x == null)
111:                    throw new IllegalArgumentException();
112:                if (Thread.interrupted())
113:                    throw new InterruptedException();
114:
115:                synchronized (putMonitor_) {
116:                    while (emptySlots_ <= 0) {
117:                        try {
118:                            putMonitor_.wait();
119:                        } catch (InterruptedException ex) {
120:                            putMonitor_.notify();
121:                            throw ex;
122:                        }
123:                    }
124:                    insert(x);
125:                }
126:                incUsedSlots();
127:            }
128:
129:            public boolean offer(Object x, long msecs)
130:                    throws InterruptedException {
131:                if (x == null)
132:                    throw new IllegalArgumentException();
133:                if (Thread.interrupted())
134:                    throw new InterruptedException();
135:
136:                synchronized (putMonitor_) {
137:                    long start = (msecs <= 0) ? 0 : System.currentTimeMillis();
138:                    long waitTime = msecs;
139:                    while (emptySlots_ <= 0) {
140:                        if (waitTime <= 0)
141:                            return false;
142:                        try {
143:                            putMonitor_.wait(waitTime);
144:                        } catch (InterruptedException ex) {
145:                            putMonitor_.notify();
146:                            throw ex;
147:                        }
148:                        waitTime = msecs - (System.currentTimeMillis() - start);
149:                    }
150:                    insert(x);
151:                }
152:                incUsedSlots();
153:                return true;
154:            }
155:
156:            public Object take() throws InterruptedException {
157:                if (Thread.interrupted())
158:                    throw new InterruptedException();
159:                Object old = null;
160:                synchronized (this ) {
161:                    while (usedSlots_ <= 0) {
162:                        try {
163:                            wait();
164:                        } catch (InterruptedException ex) {
165:                            notify();
166:                            throw ex;
167:                        }
168:                    }
169:                    old = extract();
170:                }
171:                incEmptySlots();
172:                return old;
173:            }
174:
175:            public Object poll(long msecs) throws InterruptedException {
176:                if (Thread.interrupted())
177:                    throw new InterruptedException();
178:                Object old = null;
179:                synchronized (this ) {
180:                    long start = (msecs <= 0) ? 0 : System.currentTimeMillis();
181:                    long waitTime = msecs;
182:
183:                    while (usedSlots_ <= 0) {
184:                        if (waitTime <= 0)
185:                            return null;
186:                        try {
187:                            wait(waitTime);
188:                        } catch (InterruptedException ex) {
189:                            notify();
190:                            throw ex;
191:                        }
192:                        waitTime = msecs - (System.currentTimeMillis() - start);
193:
194:                    }
195:                    old = extract();
196:                }
197:                incEmptySlots();
198:                return old;
199:            }
200:
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.