KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > cluster > tcp > ThreadPool


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.catalina.cluster.tcp;
18 import java.util.List JavaDoc;
19 import java.util.LinkedList JavaDoc;
20
21 /**
22  * @author not attributable
23  * @version 1.0
24  */

25
26 public class ThreadPool
27 {
28     /**
29      * A very simple thread pool class. The pool size is set at
30      * construction time and remains fixed. Threads are cycled
31      * through a FIFO idle queue.
32      */

33
34     List JavaDoc idle = new LinkedList JavaDoc();
35     Object JavaDoc mutex = new Object JavaDoc();
36     Object JavaDoc interestOpsMutex = null;
37
38     ThreadPool (int poolSize, Class JavaDoc threadClass, Object JavaDoc interestOpsMutex) throws Exception JavaDoc {
39         // fill up the pool with worker threads
40
this.interestOpsMutex = interestOpsMutex;
41         for (int i = 0; i < poolSize; i++) {
42             WorkerThread thread = (WorkerThread)threadClass.newInstance();
43             thread.setPool(this);
44
45             // set thread name for debugging, start it
46
thread.setName (threadClass.getName()+"[" + (i + 1)+"]");
47             thread.setDaemon(true);
48             thread.setPriority(Thread.MAX_PRIORITY);
49             thread.start();
50
51             idle.add (thread);
52         }
53     }
54
55     /**
56      * Find an idle worker thread, if any. Could return null.
57      */

58     WorkerThread getWorker()
59     {
60         WorkerThread worker = null;
61
62         
63         synchronized (mutex) {
64             while ( worker == null ) {
65                 if (idle.size() > 0) {
66                     try {
67                         worker = (WorkerThread) idle.remove(0);
68                     } catch (java.util.NoSuchElementException JavaDoc x) {
69                         //this means that there are no available workers
70
worker = null;
71                     }
72                 } else {
73                     try { mutex.wait(); } catch ( java.lang.InterruptedException JavaDoc x ) {}
74                 }
75             }
76         }
77
78         return (worker);
79     }
80
81     /**
82      * Called by the worker thread to return itself to the
83      * idle pool.
84      */

85     void returnWorker (WorkerThread worker)
86     {
87         synchronized (mutex) {
88             idle.add (worker);
89             mutex.notify();
90         }
91     }
92     public Object JavaDoc getInterestOpsMutex() {
93         return interestOpsMutex;
94     }
95 }
96
Popular Tags