1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of Java-Common.
5 *
6 * Java-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Java-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.util;
21
22 import java.util.concurrent.LinkedBlockingQueue;
23 import java.util.concurrent.ThreadFactory;
24 import java.util.concurrent.ThreadPoolExecutor;
25 import java.util.concurrent.TimeUnit;
26
27
28 /**
29 * A default implementation of thread pool which delegates to Java concurrent
30 * library. It creates worker threads with <code>Thread.NORM_PRIORITY -
31 * 2</code>. A maximum number of workers can be active concurrently,
32 * specifiable by the <code>maximumPoolSize</code> property. If
33 * <code>execute()</code> is called and no worker is free, the call will block
34 * until one of the workers terminate. Use this service to execute short
35 * tasks. In this way excessive construction of new thread instances can be
36 * prevented, speeding up the application.
37 *
38 * @author Gasper Tkacik
39 * @version $id$
40 *
41 * @deprecated This implementation has been replaced by java.util.concurent package
42 */
43 public class ThreadPool extends ThreadPoolExecutor
44 {
45 private final static long WAIT_FOR_SHUTDOWN = 3000;
46 /**
47 * Creates threads belonging to the "ThreadPoolWorker" group, with name
48 * "DefaultThreadPoolWorker" and priority of normal less by 2.
49 */
50 static private class ThreadFactoryImpl implements ThreadFactory
51 {
52 private int priority = Thread.NORM_PRIORITY - 2;
53 private ThreadGroup group = null;
54
55 /**
56 * Creates a new ThreadFactoryImpl object.
57 *
58 * @param priority priority for new worker threads
59 */
60 public ThreadFactoryImpl(int priority)
61 {
62 this.priority = priority;
63 group= new ThreadGroup("DefaultThreadPoolWorkerGroup");
64 }
65
66 /**
67 * Creates a new ThreadFactoryImpl object.
68 */
69 public ThreadFactoryImpl()
70 {
71 group= new ThreadGroup("DefaultThreadPoolWorkerGroup");
72 }
73
74 /**
75 * Creates a new thread instance.
76 *
77 * @param command the command that should be executed by the thread
78 *
79 * @return new worker thread
80 */
81 public Thread newThread(Runnable command)
82 {
83 Thread retVal = new Thread(group, command, "DefaultThreadPoolWorker");
84 retVal.setPriority(priority);
85
86 return retVal;
87 }
88 }
89
90 /**
91 * Constructs a executor with 25 active threads.
92 */
93 public ThreadPool()
94 {
95 super(25,25,1000,TimeUnit.MILLISECONDS,new LinkedBlockingQueue(),new ThreadFactoryImpl());
96 }
97
98 /**
99 * Creates a new ThreadPool object.
100 *
101 * @param priority the priority of worker threads.
102 */
103 public ThreadPool(int priority)
104 {
105 super(25,25,1000,TimeUnit.MILLISECONDS,new LinkedBlockingQueue(),new ThreadFactoryImpl(priority));
106 }
107
108 /**
109 * Returns the thread group to which all worker threads of this service
110 * belong. This will return non-<code>null</code> after this component has
111 * been initialized.
112 *
113 * @return the group of all worker threads
114 */
115 public ThreadGroup getGroup()
116 {
117 return ((ThreadFactoryImpl)getThreadFactory()).group;
118 }
119
120 /**
121 * Releases the thread pool. First attempts to wait until the tasks have
122 * executed. New tasks are not accepted. Then, if after the period
123 * <code>WAIT_FOR_SHUTDOWN</code> the threads do not complete, the pool is
124 * forcibly terminated.
125 */
126 public void destroy()
127 {
128 shutdown();
129 }
130
131 /**
132 * Returns a short summary of this instance.
133 *
134 * @return internal state of this
135 */
136 public String toString()
137 {
138 StringBuffer sbuff = new StringBuffer(200);
139 sbuff.append("ThreadPool = { currentSize=");
140 sbuff.append(getPoolSize());
141 sbuff.append(" maxSize=");
142 sbuff.append(getMaximumPoolSize());
143 sbuff.append(" coreSize=");
144 sbuff.append(getCorePoolSize());
145 sbuff.append(" } ");
146
147 return new String(sbuff);
148 }
149 }
150
151 /* __oOo__ */