1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.commons.threadpool; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public class DefaultThreadPool implements Runnable, ThreadPool { |
26 | |
|
27 | 0 | private MTQueue queue = new MTQueue(); |
28 | 0 | private boolean stopped = false; |
29 | |
private final ThreadPoolMonitor monitor; |
30 | |
private ThreadGroup threadGroup; |
31 | |
|
32 | |
public DefaultThreadPool(ThreadPoolMonitor monitor, |
33 | 0 | int numberOfThreads, int threadPriority) { |
34 | 0 | this.monitor = monitor; |
35 | 0 | for ( int i = 0; i < numberOfThreads; i++ ) { |
36 | 0 | startThread(threadPriority); |
37 | |
} |
38 | 0 | } |
39 | |
|
40 | |
public DefaultThreadPool(ThreadPoolMonitor monitor, |
41 | 0 | int numberOfThreads) { |
42 | 0 | this.monitor = monitor; |
43 | 0 | for ( int i = 0; i < numberOfThreads; i++ ) { |
44 | 0 | startThread(); |
45 | |
} |
46 | 0 | } |
47 | |
|
48 | 0 | public DefaultThreadPool() { |
49 | 0 | this.monitor = new CommonsLoggingThreadPoolMonitor(); |
50 | |
|
51 | 0 | startThread(); |
52 | 0 | } |
53 | |
|
54 | |
public DefaultThreadPool(int numberOfThreads) { |
55 | 0 | this(new CommonsLoggingThreadPoolMonitor(), numberOfThreads); |
56 | 0 | } |
57 | |
|
58 | |
public DefaultThreadPool(int numberOfThreads, int threadPriority) { |
59 | 0 | this(new CommonsLoggingThreadPoolMonitor(), numberOfThreads, threadPriority); |
60 | 0 | } |
61 | |
|
62 | |
public void setThreadGroup(ThreadGroup threadGroup) { |
63 | 0 | this.threadGroup = threadGroup; |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
public Thread startThread() { |
68 | 0 | Thread thread = createThread(); |
69 | 0 | thread.start(); |
70 | 0 | return thread; |
71 | |
} |
72 | |
|
73 | |
public Thread startThread(int priority) { |
74 | 0 | Thread thread = createThread(); |
75 | 0 | thread.setPriority(priority); |
76 | 0 | thread.start(); |
77 | 0 | return thread; |
78 | |
} |
79 | |
|
80 | |
private Thread createThread() { |
81 | 0 | if(this.threadGroup != null) { |
82 | 0 | return new Thread(this.threadGroup, this); |
83 | |
} else { |
84 | 0 | return new Thread(this); |
85 | |
} |
86 | |
} |
87 | |
|
88 | |
public void stop() { |
89 | 0 | stopped = true; |
90 | 0 | } |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public int getRunnableCount() { |
96 | 0 | return queue.size(); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void invokeLater(Runnable task) { |
108 | 0 | queue.add( task ); |
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public void run() { |
117 | 0 | while ( ! stopped ) { |
118 | 0 | Runnable task = (Runnable) queue.remove(); |
119 | 0 | if ( task != null ) { |
120 | |
try { |
121 | 0 | task.run(); |
122 | |
} |
123 | 0 | catch (Throwable t) { |
124 | 0 | monitor.handleThrowable(this.getClass(), task, t); |
125 | 0 | } |
126 | |
} |
127 | 0 | } |
128 | 0 | } |
129 | |
} |