KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > ThreadPool


1 package org.sapia.ubik.net;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6
7 /**
8  * A pool of <code>PooledThread</code> instances. Inheriting classes
9  * must implement the <code>newThread()</code> method, which must return
10  * an application-specific <code>PooledThread</code> instance.
11  * <p>
12  * Applications must use the pool in the following manner:
13  *
14  * <pre>
15  *
16  * PooledThread thread = (PooledThread)threadPool.acquire();
17  *
18  * thread.exec(someData);
19  *
20  * </pre>
21  *
22  * Upon the <code>exec()</code> method being called, the thread
23  * immediately:
24  * <ul>
25  * <li>calls its own <code>doExec</code> method;
26  * <li>releases itself to its "owning" pool after the doExec() method returns.
27  * </ul>
28  *
29  * <p>
30  * Thus, applications need not be concerned about returning the passed in thread
31  * to the pool.
32  *
33  *
34  *
35  * @see PooledThread
36  * @see PooledThread#doExec(Object)
37  * @author Yanick Duchesne
38  * <dl>
39  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
40  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
41  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
42  * </dl>
43  */

44 public abstract class ThreadPool extends Pool {
45   private String JavaDoc _name;
46   private boolean _daemon;
47   private boolean _shuttingDown;
48   private List JavaDoc _busy = new ArrayList JavaDoc();
49
50   /**
51    * Creates a thread pool.
52    *
53    * @param name the name of the threads that will be created (to distinguish
54    * among the different threads, a counter is appended to the name for
55    * each thread).
56    *
57    * @param daemon if <code>true</code>, the threads created by this pool will
58    * be set as daemon.
59    *
60    * @param maxSize the maximum number of threads that this pool creates.
61    */

62   protected ThreadPool(String JavaDoc name, boolean daemon, int maxSize) {
63     super(maxSize);
64     _name = name;
65     _daemon = daemon;
66   }
67
68   /**
69    * Creates a thread pool.
70    *
71    * @param name the name of the threads that will be created (to distinguish
72    * among the different threads, a counter is appended to the name for
73    * each thread).
74    *
75    * @param daemon if <code>true</code>, the threads created by this pool will
76    * be set as daemon.
77    */

78   protected ThreadPool(String JavaDoc name, boolean daemon) {
79     _name = name;
80     _daemon = daemon;
81   }
82
83   /**
84    * @see org.sapia.ubik.net.Pool#onAcquire(Object)
85    *
86    * @throws IllegalStateException if this instance is shutting down or is shut down.
87    */

88   protected Object JavaDoc onAcquire(Object JavaDoc o) throws Exception JavaDoc, IllegalStateException JavaDoc {
89     if (_shuttingDown) {
90       throw new IllegalStateException JavaDoc(
91         "Could not acquire thread; pool is shutting down");
92     }
93
94     ((PooledThread) o).acquire();
95     _busy.add(o);
96
97     return o;
98   }
99
100   /**
101    * @see org.sapia.ubik.net.Pool#onRelease(Object)
102    */

103   protected synchronized void onRelease(Object JavaDoc o) {
104     ((PooledThread) o).release();
105     _busy.remove(o);
106     notifyAll();
107   }
108
109   /**
110    * Cleanly shuts down this instance.
111    *
112    * @see #shutdown(long)
113    */

114   public synchronized void shutdown() {
115     shutdown(0);
116   }
117
118   /**
119    * Cleanly shuts down this instance; internally busy threads
120    * are interrupted - currently executing threads finish their
121    * task before termination.
122    * <p>
123    * This method waits that all threads are finished before it
124    * returns, OR until the given timeout is reached.
125    *
126    * @param timeout a shutdown timeout - this method will return
127    * when this timeout is reached, even if some threads are still
128    * executing.
129    */

130   public synchronized void shutdown(long timeout) {
131     _shuttingDown = true;
132
133     for (int i = 0; i < _objects.size(); i++) {
134       ((PooledThread) _objects.get(i)).shutdown();
135     }
136
137     if (_busy.size() > 0) {
138       for (int i = 0; i < _busy.size(); i++) {
139         ((PooledThread) _busy.get(i)).shutdown();
140       }
141
142       Timer timer = new Timer(timeout);
143
144       while (_busy.size() != 0) {
145         try {
146           wait(timeout);
147
148           if (timer.isOver()) {
149             break;
150           }
151         } catch (InterruptedException JavaDoc e) {
152           return;
153         }
154       }
155     }
156   }
157
158   /**
159    * @see org.sapia.ubik.net.Pool#doNewObject()
160    */

161   protected final Object JavaDoc doNewObject() throws Exception JavaDoc {
162     PooledThread th = (PooledThread) newThread();
163     th.setOwner(this);
164     th.setName("[" + _name + " - " + super.getCreatedCount() + "]");
165     th.setDaemon(_daemon);
166     th.start();
167
168     return th;
169   }
170
171   /**
172    * This method must be overridden by inheriting classes; the returned
173    * thread must not be started by this method; the pool implements this
174    * behavior.
175    *
176    * @return a <code>PooledThread</code> instance.
177    */

178   protected abstract PooledThread newThread() throws Exception JavaDoc;
179 }
180
Popular Tags