View Javadoc

1   /**********************************************************************
2    * ThreadPool.java
3    * created on 07.08.2004 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/concurrent/ThreadPool.java,v $
5    * $Date: 2006/02/12 12:48:00 $
6    * $Revision: 1.13 $
7    *
8    * ====================================================================
9    *
10   *  Copyright 2005-2006 netseeker aka Michael Manske
11   *
12   *  Licensed under the Apache License, Version 2.0 (the "License");
13   *  you may not use this file except in compliance with the License.
14   *  You may obtain a copy of the License at
15   *
16   *      http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *  Unless required by applicable law or agreed to in writing, software
19   *  distributed under the License is distributed on an "AS IS" BASIS,
20   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *  See the License for the specific language governing permissions and
22   *  limitations under the License.
23   * ====================================================================
24   *
25   * This file is part of the ejoe framework.
26   * For more information on the author, please see
27   * <http://www.manskes.de/>.
28   *
29   *********************************************************************/
30  
31  package de.netseeker.ejoe.concurrent;
32  
33  /***
34   * @author netseeker aka Michael Manske
35   * @since 0.3.0
36   */
37  public final class ThreadPool implements Runnable
38  {
39  	private final ThreadQueue	queue	= new ThreadQueue();
40  
41  	private boolean				stopped	= false;
42  
43  	private ThreadGroup			threadGroup;
44  
45  	/***
46  	 * @param numberOfThreads
47  	 * @param threadPriority
48  	 */
49  	public ThreadPool(int numberOfThreads, int threadPriority)
50  	{
51  		for (int i = 0; i < numberOfThreads; i++)
52  		{
53  			startThread(threadPriority);
54  		}
55  	}
56  
57  	/***
58  	 * @param numberOfThreads
59  	 */
60  	public ThreadPool(int numberOfThreads)
61  	{
62  		for (int i = 0; i < numberOfThreads; i++)
63  		{
64  			startThread();
65  		}
66  	}
67  
68  	/***
69  	 *
70  	 */
71  	public ThreadPool()
72  	{
73  		// typically a thread pool should have at least 1 thread
74  		startThread();
75  	}
76  
77  	/***
78  	 * @param threadGroup
79  	 */
80  	public ThreadPool(final ThreadGroup threadGroup)
81  	{
82  		this.threadGroup = threadGroup;
83  		startThread();
84  	}
85  
86  	/***
87  	 * @param threadGroup
88  	 * @param numberOfThreads
89  	 */
90  	public ThreadPool(final ThreadGroup threadGroup, int numberOfThreads)
91  	{
92  		this.threadGroup = threadGroup;
93  		for (int i = 0; i < numberOfThreads; i++)
94  		{
95  			startThread();
96  		}
97  	}
98  
99  	/***
100 	 * @param threadGroup
101 	 */
102 	public void setThreadGroup(final ThreadGroup threadGroup)
103 	{
104 		this.threadGroup = threadGroup;
105 	}
106 
107 	/*** Start a new thread running */
108 	public Thread startThread()
109 	{
110 		Thread thread = createThread();
111 		thread.start();
112 		return thread;
113 	}
114 
115 	/***
116 	 * @param priority
117 	 * @return
118 	 */
119 	public Thread startThread(int priority)
120 	{
121 		Thread thread = createThread();
122 		thread.setPriority(priority);
123 		thread.start();
124 		return thread;
125 	}
126 
127 	private Thread createThread()
128 	{
129 		if (this.threadGroup != null)
130 		{
131 			return new Thread(this.threadGroup, this);
132 		}
133 		else
134 		{
135 			return new Thread(this);
136 		}
137 	}
138 
139 	public void stop()
140 	{
141 		stopped = true;
142 	}
143 
144 	/***
145 	 * Returns number of runnable object in the queue.
146 	 */
147 	public int getRunnableCount()
148 	{
149 		return queue.size();
150 	}
151 
152 	/***
153 	 * Dispatch a new task onto this pool to be invoked asynchronously later
154 	 */
155 	public void invokeLater(Runnable task)
156 	{
157 		queue.add(task);
158 	}
159 
160 	/***
161 	 * The method ran by the pool of background threads
162 	 */
163 	public void run()
164 	{
165 		while (!stopped)
166 		{
167 			Runnable task = queue.remove();
168 			if (task != null)
169 			{
170 				task.run();
171 			}
172 		}
173 	}
174 }