KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > examples > util > concurrent > AsynchronousManager


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package examples.util.concurrent;
9
10 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
11 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
12 import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
13 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException;
14 import examples.util.definition.Definition;
15
16 /**
17  * Manages the thread pool for all the asynchronous invocations.
18  *
19  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
20  */

21 public class AsynchronousManager {
22
23     protected static final AsynchronousManager INSTANCE = new AsynchronousManager();
24
25     protected PooledExecutor m_threadPool = null;
26
27     protected boolean m_initialized = false;
28
29     /**
30      * Executes a task in a thread from the thread pool.
31      *
32      * @param task the task to execute (Runnable)
33      */

34     public void execute(final Runnable JavaDoc task) {
35         if (notInitialized()) {
36             throw new IllegalStateException JavaDoc("asynchronous thread pool not initialized");
37         }
38         try {
39             m_threadPool.execute(task);
40         } catch (InterruptedException JavaDoc e) {
41             Thread.currentThread().interrupt();
42             notifyAll();
43             throw new WrappedRuntimeException(e);
44         } catch (Exception JavaDoc e) {
45             throw new WrappedRuntimeException(e);
46         }
47     }
48
49     /**
50      * Returns the one A only AsynchronousManager instance.
51      *
52      * @return the asynchronous manager
53      */

54     public static AsynchronousManager getInstance() {
55         return INSTANCE;
56     }
57
58     /**
59      * Initializes the thread pool.
60      *
61      * @param def the definition
62      */

63     public synchronized void initialize(final Definition definition) {
64         if (definition == null) {
65             return;
66         }
67         if (m_initialized) {
68             return;
69         }
70         examples.util.definition.ThreadPoolDefinition def = (examples.util.definition.ThreadPoolDefinition) definition;
71         int threadPoolMaxSize = def.getMaxSize();
72         int threadPoolInitSize = def.getInitSize();
73         int threadPoolMinSize = def.getMinSize();
74         int keepAliveTime = def.getKeepAliveTime();
75         boolean waitWhenBlocked = def.getWaitWhenBlocked();
76         boolean bounded = def.getBounded();
77         if (threadPoolMaxSize < threadPoolInitSize || threadPoolMaxSize < threadPoolMinSize) {
78             throw new IllegalArgumentException JavaDoc("max size of thread pool can not exceed the init size");
79         }
80
81         // if threadPoolMaxSize is -1 or less => no maximum limit
82
// if keepAliveTime is -1 or less => threads are alive forever, i.e no timeout
83
if (bounded) {
84             createBoundedThreadPool(
85                     threadPoolMaxSize,
86                     threadPoolMinSize,
87                     threadPoolInitSize,
88                     keepAliveTime,
89                     waitWhenBlocked
90             );
91         } else {
92             createDynamicThreadPool(threadPoolMinSize, threadPoolInitSize, keepAliveTime);
93         }
94         m_initialized = true;
95     }
96
97     /**
98      * Closes down the thread pool.
99      */

100     public void stop() {
101         m_threadPool.shutdownNow();
102     }
103
104     /**
105      * Creates a bounded thread pool.
106      *
107      * @param threadPoolMaxSize
108      * @param threadPoolMinSize
109      * @param threadPoolInitSize
110      * @param keepAliveTime
111      * @param waitWhenBlocked
112      */

113     protected void createBoundedThreadPool(final int threadPoolMaxSize,
114                                            final int threadPoolMinSize,
115                                            final int threadPoolInitSize,
116                                            final int keepAliveTime,
117                                            final boolean waitWhenBlocked) {
118         m_threadPool = new PooledExecutor(new BoundedBuffer(threadPoolInitSize), threadPoolMaxSize);
119         m_threadPool.setKeepAliveTime(keepAliveTime);
120         m_threadPool.createThreads(threadPoolInitSize);
121         m_threadPool.setMinimumPoolSize(threadPoolMinSize);
122         if (waitWhenBlocked) {
123             m_threadPool.waitWhenBlocked();
124         }
125     }
126
127     /**
128      * Creates a dynamic thread pool.
129      *
130      * @param threadPoolMinSize
131      * @param threadPoolInitSize
132      * @param keepAliveTime
133      */

134     protected void createDynamicThreadPool(final int threadPoolMinSize,
135                                            final int threadPoolInitSize,
136                                            final int keepAliveTime) {
137         m_threadPool = new PooledExecutor(new LinkedQueue());
138         m_threadPool.setKeepAliveTime(keepAliveTime);
139         m_threadPool.createThreads(threadPoolInitSize);
140         m_threadPool.setMinimumPoolSize(threadPoolMinSize);
141     }
142
143     /**
144      * Checks if the service has been initialized.
145      *
146      * @return boolean
147      */

148     protected boolean notInitialized() {
149         return !m_initialized;
150     }
151
152     /**
153      * Private constructor.
154      */

155     protected AsynchronousManager() {
156     }
157 }
Popular Tags