1
2
3
|
public interface Executor{ void executor(Runnable command); } |
如上所寫,Executor實際上是一個接口,他提供了唯一的接口方法executor(Runnable command)
Executor實際上是提供了一個線程池的概念, 他的優點是實現了多線程任務的提交和執行的解耦。 通過Executor,使用者可以不用關心任務是被哪一個線程執行的,什么時候執行的。只需要等待線程執行完畢,并獲得最后的結果就可以了。舉個簡答的例子:
我們工作中當老大的老大(且稱作LD^2)把一個任務交給我們老大(LD)的時候,到底是LD自己干,還是轉過身來拉來一幫苦逼的兄弟加班加點干,那LD^2是不管的。LD^2只用把人描述清楚提及給LD,然后喝著咖啡等著收LD的report即可。等LD一封郵件非常優雅
地報告LD^2report結果時,實際操作中是碼農A和碼農B干了一個月,還是碼農ABCDE加班干了一個禮拜,大多是不用體現的。這套機制的優點就是LD^2找個合適的LD出來提交任務即可,接口友好有效,不用為具體怎么干費神費力。
-----(戲(細)說Executor框架線程池任務執行全過程)
下面是一個簡單的套用Executor的例子:
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
|
package concurrency.practice; package com.journaldev.threadpool; public class WorkerThread implements Runnable { private String command; public WorkerThread(String s){ this .command=s; } @Override public void run() { System.out.println(Thread.currentThread().getName()+ " Start. Command = " +command); processCommand(); System.out.println(Thread.currentThread().getName()+ " End." ); } private void processCommand() { try { Thread.sleep( 5000 ); } catch (InterruptedException e) { e.printStackTrace(); } } @Override public String toString(){ return this .command; } } |
Here is the test program where we are creating fixed thread pool from Executors framework.
SimpleThreadPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.journaldev.threadpool; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SimpleThreadPool { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool( 5 ); for ( int i = 0 ; i < 10 ; i++) { Runnable worker = new WorkerThread( "" + i); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println( "Finished all threads" ); } } |
讓我們查看一下輸出結果:
Here is the output of the above program.
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 Start. Command = 1 pool- 1 -thread- 4 Start. Command = 3 pool- 1 -thread- 1 Start. Command = 0 pool- 1 -thread- 3 Start. Command = 2 pool- 1 -thread- 5 Start. Command = 4 pool- 1 -thread- 4 End. pool- 1 -thread- 5 End. pool- 1 -thread- 1 End. pool- 1 -thread- 3 End. pool- 1 -thread- 3 Start. Command = 8 pool- 1 -thread- 2 End. pool- 1 -thread- 2 Start. Command = 9 pool- 1 -thread- 1 Start. Command = 7 pool- 1 -thread- 5 Start. Command = 6 pool- 1 -thread- 4 Start. Command = 5 pool- 1 -thread- 2 End. pool- 1 -thread- 4 End. pool- 1 -thread- 3 End. pool- 1 -thread- 5 End. pool- 1 -thread- 1 End. Finished all threads |
In above program, we are creating fixed size thread pool of 5 worker threads. Then we are submitting 10 jobs to this pool, since the pool size is 5, it will start working on 5 jobs and other jobs will be in wait state, as soon as one of the job is finished, another job from the wait queue will be picked up by worker thread and get’s executed.
注意在 SimpleThreadPool.java 中我們調用了ExecutorService 接口。該接口實現了Executor并且提供了一個額外的方法
1
|
public interface ExecutorService extends Executor |
原文地址:https://www.cnblogs.com/CBDoctor/p/5078199.html