/Users/lyon/j4p/src/classUtils/pack/util/tp/ThreadPoolThread.java
|
1 package classUtils.pack.util.tp;
2
3 import java.io.*;
4
5 /**
6 * A thread which acts as a pool for
7 * other threads. Its run methods simply waits if there aren't
8 * runnable start requests.
9 * <p>
10 * Albeit it is not type-compatible with {@link ThreadPool ThreadPool} (since
11 * it extends Thread) this class exposes the same methods.
12 *
13 * @author Cris Sadun
14 * @version 1.0
15 */
16 public class ThreadPoolThread extends Thread implements classUtils.pack.util.Terminable {
17
18 private ThreadPool tp;
19 private boolean shutdown=false;
20
21 /**
22 * Create a pool with the given size using the given queue object.
23 * @param size the size of the pool
24 * @param daemon if <b>true</b> the pools will be daemon
25 * @param queue the queue to use for determining the next thread to
26 * instantiate when there are pending start requests.
27 *
28 */
29 public ThreadPoolThread(int size, boolean daemon, Queue queue) {
30 tp=new ThreadPool(size, daemon, queue);
31 }
32
33 /**
34 * Create a pool with the given size with a FIFO queue
35 * @param size the size of the pool
36 * @param daemon if <b>true</b> the pools will be daemon
37 */
38 public ThreadPoolThread(int size, boolean daemon) {
39 tp = new ThreadPool(size, daemon);
40 }
41
42 /**
43 * Create a pool of daemon threads with the given size and a FIFO waiting queue
44 * @param size the size of the pool
45 */
46 public ThreadPoolThread(int size) {
47 tp = new ThreadPool(size);
48 }
49
50 /**
51 * Return the size of the pool
52 * @return the size of the pool
53 */
54 public int size() { return tp.size(); }
55
56 /**
57 * Return the number of thread currently queued
58 * @return the number of thread currently queued
59 */
60 public int getQueueSize() { return tp.getQueueSize(); }
61
62 /**
63 * Adds a runnable object to the pool.
64 * If there's a thread available, the runnable is associated to the thread
65 * and started. Else, it is queued, and will run as soon as one thread becomes
66 * available.
67 * @param runnable the Runnable object to execute
68 * @return <b>true</b> if the runnable is started, </b>false</b> if it's queued.
69 */
70 public boolean start(Runnable runnable) {
71 return tp.start(runnable);
72 }
73
74 public void shutdown() { shutdown=true; }
75
76 public boolean isShuttingDown() { return shutdown; }
77
78 public synchronized void run() {
79 while(!shutdown) {
80 }
81 }
82 }