Source Code Cross Referenced for AsynchronousManager.java in  » Aspect-oriented » aspectwerkz-2.0 » examples » 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 » Aspect oriented » aspectwerkz 2.0 » examples.util.concurrent 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**************************************************************************************
002:         * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved.                 *
003:         * http://aspectwerkz.codehaus.org                                                    *
004:         * ---------------------------------------------------------------------------------- *
005:         * The software in this package is published under the terms of the LGPL license      *
006:         * a copy of which has been included with this distribution in the license.txt file.  *
007:         **************************************************************************************/package examples.util.concurrent;
008:
009:        import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
010:        import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
011:        import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
012:        import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
013:        import examples.util.definition.Definition;
014:
015:        /**
016:         * Manages the thread pool for all the asynchronous invocations.
017:         *
018:         * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
019:         */
020:        public class AsynchronousManager {
021:
022:            protected static final AsynchronousManager INSTANCE = new AsynchronousManager();
023:
024:            protected PooledExecutor m_threadPool = null;
025:
026:            protected boolean m_initialized = false;
027:
028:            /**
029:             * Executes a task in a thread from the thread pool.
030:             *
031:             * @param task the task to execute (Runnable)
032:             */
033:            public void execute(final Runnable task) {
034:                if (notInitialized()) {
035:                    throw new IllegalStateException(
036:                            "asynchronous thread pool not initialized");
037:                }
038:                try {
039:                    m_threadPool.execute(task);
040:                } catch (InterruptedException e) {
041:                    Thread.currentThread().interrupt();
042:                    notifyAll();
043:                    throw new WrappedRuntimeException(e);
044:                } catch (Exception e) {
045:                    throw new WrappedRuntimeException(e);
046:                }
047:            }
048:
049:            /**
050:             * Returns the one A only AsynchronousManager instance.
051:             *
052:             * @return the asynchronous manager
053:             */
054:            public static AsynchronousManager getInstance() {
055:                return INSTANCE;
056:            }
057:
058:            /**
059:             * Initializes the thread pool.
060:             *
061:             * @param def the definition
062:             */
063:            public synchronized void initialize(final Definition definition) {
064:                if (definition == null) {
065:                    return;
066:                }
067:                if (m_initialized) {
068:                    return;
069:                }
070:                examples.util.definition.ThreadPoolDefinition def = (examples.util.definition.ThreadPoolDefinition) definition;
071:                int threadPoolMaxSize = def.getMaxSize();
072:                int threadPoolInitSize = def.getInitSize();
073:                int threadPoolMinSize = def.getMinSize();
074:                int keepAliveTime = def.getKeepAliveTime();
075:                boolean waitWhenBlocked = def.getWaitWhenBlocked();
076:                boolean bounded = def.getBounded();
077:                if (threadPoolMaxSize < threadPoolInitSize
078:                        || threadPoolMaxSize < threadPoolMinSize) {
079:                    throw new IllegalArgumentException(
080:                            "max size of thread pool can not exceed the init size");
081:                }
082:
083:                // if threadPoolMaxSize is -1 or less => no maximum limit
084:                // if keepAliveTime is -1 or less => threads are alive forever, i.e no timeout
085:                if (bounded) {
086:                    createBoundedThreadPool(threadPoolMaxSize,
087:                            threadPoolMinSize, threadPoolInitSize,
088:                            keepAliveTime, waitWhenBlocked);
089:                } else {
090:                    createDynamicThreadPool(threadPoolMinSize,
091:                            threadPoolInitSize, keepAliveTime);
092:                }
093:                m_initialized = true;
094:            }
095:
096:            /**
097:             * Closes down the thread pool.
098:             */
099:            public void stop() {
100:                m_threadPool.shutdownNow();
101:            }
102:
103:            /**
104:             * Creates a bounded thread pool.
105:             *
106:             * @param threadPoolMaxSize
107:             * @param threadPoolMinSize
108:             * @param threadPoolInitSize
109:             * @param keepAliveTime
110:             * @param waitWhenBlocked
111:             */
112:            protected void createBoundedThreadPool(final int threadPoolMaxSize,
113:                    final int threadPoolMinSize, final int threadPoolInitSize,
114:                    final int keepAliveTime, final boolean waitWhenBlocked) {
115:                m_threadPool = new PooledExecutor(new BoundedBuffer(
116:                        threadPoolInitSize), threadPoolMaxSize);
117:                m_threadPool.setKeepAliveTime(keepAliveTime);
118:                m_threadPool.createThreads(threadPoolInitSize);
119:                m_threadPool.setMinimumPoolSize(threadPoolMinSize);
120:                if (waitWhenBlocked) {
121:                    m_threadPool.waitWhenBlocked();
122:                }
123:            }
124:
125:            /**
126:             * Creates a dynamic thread pool.
127:             *
128:             * @param threadPoolMinSize
129:             * @param threadPoolInitSize
130:             * @param keepAliveTime
131:             */
132:            protected void createDynamicThreadPool(final int threadPoolMinSize,
133:                    final int threadPoolInitSize, final int keepAliveTime) {
134:                m_threadPool = new PooledExecutor(new LinkedQueue());
135:                m_threadPool.setKeepAliveTime(keepAliveTime);
136:                m_threadPool.createThreads(threadPoolInitSize);
137:                m_threadPool.setMinimumPoolSize(threadPoolMinSize);
138:            }
139:
140:            /**
141:             * Checks if the service has been initialized.
142:             *
143:             * @return boolean
144:             */
145:            protected boolean notInitialized() {
146:                return !m_initialized;
147:            }
148:
149:            /**
150:             * Private constructor.
151:             */
152:            protected AsynchronousManager() {
153:            }
154:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.