KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > ThreadPool


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.web;
9
10 import java.util.Stack JavaDoc;
11
12 /**
13  * A simple thread pool.
14  *
15  * <a HREF="mailto:rickard.oberg@telkel.com">Rickard Öberg</a>
16  * @version $Revision: 1.9 $
17  */

18 public class ThreadPool
19 {
20    // Constants -----------------------------------------------------
21

22    // Attributes ----------------------------------------------------
23

24    /**
25     * Stack of idle threads cached for future use.
26     */

27    private final Stack JavaDoc pool = new Stack JavaDoc();
28
29    /**
30     * Maximum number of idle threads cached in this pool.
31     */

32    private int maxSize = 10;
33
34
35    private boolean enabled = false;
36
37    // Static --------------------------------------------------------
38

39    // Constructors --------------------------------------------------
40

41    /**
42     * Create a new pool.
43     */

44    public ThreadPool()
45    {
46    }
47
48    // Public --------------------------------------------------------
49
public synchronized void enable()
50    {
51       enabled = true;
52    }
53
54    public synchronized void disable()
55    {
56       enabled = false;
57       while (!pool.isEmpty())
58       {
59          Worker w = (Worker)pool.pop();
60          w.die();
61       } // end of while ()
62
}
63
64
65
66    /**
67     * Set the maximum number of idle threads cached in this pool.
68     */

69    public void setMaximumSize(int size)
70    {
71       maxSize = size;
72    }
73
74    /**
75     * Do some work.
76     * This will either create a new thread to do the work, or
77     * use an existing idle cached thread.
78     */

79    public synchronized void run(Runnable JavaDoc work)
80    {
81       if (pool.size() == 0) {
82          new Worker(work);
83       } else {
84          Worker w = (Worker)pool.pop();
85          w.run(work);
86       }
87    }
88
89    // Private -------------------------------------------------------
90

91    /**
92     * Return an idle worker thread to the pool of cached idle threads.
93     * This is called from the worker thread itself.
94     */

95    private synchronized void returnWorker(Worker w)
96    {
97       if (enabled && pool.size() < maxSize)
98       {
99          pool.push(w);
100       }
101       else
102       {
103          w.die();
104       } // end of else
105
}
106
107    // Inner classes -------------------------------------------------
108

109    class Worker extends Thread JavaDoc
110    {
111       /**
112        * Flags that this worker may continue to work.
113        */

114       boolean running = true;
115
116       /**
117        * Work to do, of <code>null</code> if no work to do.
118        */

119       Runnable JavaDoc work;
120
121       /**
122        * Create a new Worker to do some work.
123        */

124       Worker(Runnable JavaDoc work)
125       {
126          this.work = work;
127          setDaemon(true);
128          start();
129       }
130
131       /**
132        * Tell this worker to die.
133        */

134       public synchronized void die()
135       {
136          running = false;
137          this.notify();
138       }
139
140       /**
141        * Give this Worker some work to do.
142        *
143        * @throws IllegalStateException If this worker already
144        * has work to do.
145        */

146       public synchronized void run(Runnable JavaDoc work)
147       {
148          if (this.work != null)
149             throw new IllegalStateException JavaDoc("Worker already has work to do.");
150          this.work = work;
151          this.notify();
152       }
153
154       /**
155        * The worker loop.
156        */

157       public void run()
158       {
159          while (running) {
160             // If work is available then execute it
161
if (work != null) {
162                try {
163                   work.run();
164                } catch (Exception JavaDoc e) {
165                   //DEBUG Logger.exception(e);
166
}
167                // Clear work
168
work = null;
169             }
170
171             // Return to pool of cached idle threads
172
returnWorker(this);
173
174             // Wait for more work to become available
175
synchronized (this) {
176                while (running && work == null) {
177                   try {
178                      this.wait();
179                   } catch (InterruptedException JavaDoc e) {
180                      // Ignore
181
}
182                }
183             }
184          }
185       }
186
187    }
188 }
189
190
Popular Tags