本文转自:http://www.importnew.com/8542.html
线程池负责管理工作线程,包含一个等待执行的任务队列。线程池的任务队列是一个Runnable集合,工作线程负责从任务队列中取出并执行Runnable对象。
java.util.concurrent.executors 提供了 java.util.concurrent.executor 接口的一个Java实现,可以创建线程池。下面是一个简单示例:
首先创建一个Runable 类:
WorkerThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package
public
implements
private
public
this .command=s; } @Override public
System.out.println(Thread.currentThread().getName()+ " +command); processCommand(); System.out.println(Thread.currentThread().getName()+ " ); } private
try
Thread.sleep( 5000 ); } catch
e.printStackTrace(); } } @Override public
return
.command; } } |
下面是一个测试程序,从 Executors 框架中创建固定大小的线程池:
SimpleThreadPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package
import
import
public
public
ExecutorService 5 ); for
int
0 ; 10 ; Runnable new
""
executor.execute(worker); } executor.shutdown(); while
} System.out.println( "Finished ); } } |
在上面的程序中,我们创建了包含5个工作线程的固定大小线程池。然后,我们向线程池提交10个任务。由于线程池的大小是5,因此首先会启动5个工作线程,其他任务将进行等待。一旦有任务结束,工作线程会从等待队列中挑选下一个任务并开始执行。
以上程序的输出结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
pool- 1 -thread- 2
1 pool- 1 -thread- 4
3 pool- 1 -thread- 1
0 pool- 1 -thread- 3
2 pool- 1 -thread- 5
4 pool- 1 -thread- 4
pool- 1 -thread- 5
pool- 1 -thread- 1
pool- 1 -thread- 3
pool- 1 -thread- 3
8 pool- 1 -thread- 2
pool- 1 -thread- 2
9 pool- 1 -thread- 1
7 pool- 1 -thread- 5
6 pool- 1 -thread- 4
5 pool- 1 -thread- 2
pool- 1 -thread- 4
pool- 1 -thread- 3
pool- 1 -thread- 5
pool- 1 -thread- 1
Finished |
从输出结果看,线程池中有五个名为“pool-1-thread-1”…“pool-1-thread-5”的工作线程负责执行提交的任务。
Executors 类使用 ExecutorService 提供了一个 ThreadPoolExecutor 的简单实现,但
ThreadPoolExecutor 提供的功能远不止这些。我们可以指定创建 ThreadPoolExecutor 实例时活跃的线程数,并且可以限制线程池的大小,还可以创建自己的 RejectedExecutionHandler 实现来处理不适合放在工作队列里的任务。
下面是一个 RejectedExecutionHandler 接口的自定义实现:
RejectedExecutionHandlerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package
import
import
public
implements
@Override public
System.out.println(r.toString() " ); } } |
ThreadPoolExecutor 提供了一些方法,可以查看执行状态、线程池大小、活动线程数和任务数。所以,我通过一个监视线程在固定间隔输出执行信息。
MyMonitorThread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package
import
public
implements
{ private
private
private
true ; public
int
{ this .executor this .seconds=delay; } public
this .run= false ; } @Override public
{ while (run){ System.out.println( String.format( "[monitor] , this .executor.getPoolSize(), this .executor.getCorePoolSize(), this .executor.getActiveCount(), this .executor.getCompletedTaskCount(), this .executor.getTaskCount(), this .executor.isShutdown(), this .executor.isTerminated())); try
Thread.sleep(seconds* 1000 ); } catch
e.printStackTrace(); } } } } |
下面是使用 ThreadPoolExecutor 的线程池实现示例:
WorkerPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package
import
import
import
import
import
public
public
throws
//RejectedExecutionHandler RejectedExecutionHandlerImpl new
//Get ThreadFactory //creating ThreadPoolExecutor new
2 , 4 , 10 , new
2 ), //start MyMonitorThread new
3 ); Thread new
monitorThread.start(); //submit for ( int
0 ; 10 ; executorPool.execute( new
"cmd" +i)); } Thread.sleep( 30000 ); //shut executorPool.shutdown(); //shut Thread.sleep( 5000 ); monitor.shutdown(); } } |
请注意:在初始化 ThreadPoolExecutor 时,初始线程池大小设为2、最大值设为4、工作队列大小设为2。所以,如果当前有4个任务正在运行而此时又有新任务提交,工作队列将只存储2个任务和其他任务将交由RejectedExecutionHandlerImpl 处理。
程序执行的结果如下,确认了上面的结论:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
pool-1-thread-1 pool-1-thread-4 cmd6 pool-1-thread-3 pool-1-thread-2 cmd7 cmd8 cmd9 [monitor] /2 ] false , false [monitor] /2 ] false , false pool-1-thread-4 pool-1-thread-1 pool-1-thread-2 pool-1-thread-3 pool-1-thread-1 pool-1-thread-4 [monitor] /2 ] false , false [monitor] /2 ] false , false pool-1-thread-1 pool-1-thread-4 [monitor] /2 ] false , false [monitor] /2 ] false , false [monitor] /2 ] false , false [monitor] /2 ] false , false [monitor] /2 ] false , false [monitor] /2 ] false , false [monitor] /2 ] true , true [monitor] /2 ] true , true |
请注意活跃线程、已完成线程和任务完成总数的变化。我们可以调用 shutdown() 结束所有已提交任务并终止线程池。
如果希望延迟执行或定期运行任务,那么可以使用 ScheduledThreadPoolExecutor 类。要了解更多,请参见 Java
Schedule Thread Pool Executor。