設(shè)計(jì)理念——為何而存在
先來一發(fā)源碼棠枉。
public interface Executor {
void execute(Runnable command);
}
就是這么短。那么這么短的一個(gè)接口存在的意義是什么呢泡挺?
下面就來分析一下辈讶。
當(dāng)我們想創(chuàng)建并執(zhí)行一個(gè)線程的時(shí)候,通常是這樣的娄猫。
new Thread(new(RunnableTask())).start()
是不是短贱除!平!快媳溺!月幌。這樣不是很好嗎?但是使用這種方式悬蔽,我們無法控制程序中線程的數(shù)量扯躺,創(chuàng)建過多的線程會(huì)帶來性能問題。我們需要一種方式控制線程的數(shù)量,有時(shí)我們還希望控制各個(gè)任務(wù)之間調(diào)度關(guān)系录语。Executor接口正式為解決這個(gè)問題而來倍啥。
Executor接口官方介紹:An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads.
意思:Executor是用來提交Runnable的。它可以將Runnable的遞交澎埠、使用的細(xì)節(jié)虽缕、調(diào)度與執(zhí)行解耦。用來代替直接創(chuàng)建Thread的方式蒲稳。
不容易理解氮趋?莫關(guān)系!下面通過一個(gè)實(shí)際應(yīng)用來解釋這段話江耀。
比如剩胁,你想保證任務(wù)的順序執(zhí)行(每個(gè)時(shí)刻只有一個(gè)Runnable在執(zhí)行,只有上一個(gè)Runnable執(zhí)行完畢之后才會(huì)執(zhí)行下一個(gè)Runnable)并且為每一個(gè)Runnable創(chuàng)建一個(gè)新的Thread决记。我們可以這樣做:
//SerialExecutor提供調(diào)度功能,將用戶提交的Runnable保存到一個(gè)隊(duì)列里面倍踪,然后順序執(zhí)行
class SerialExecutor implements Executor {
//隊(duì)列系宫,用來存放提交進(jìn)來的Runnable
final Queue<Runnable> tasks = new ArrayDeque<Runnable>();
//提交進(jìn)來的Runnable實(shí)際執(zhí)行的位置。
final Executor executor;
//當(dāng)前正在執(zhí)行的Runnable
Runnable active;
SerialExecutor(Executor executor) {
this.executor = executor;
}
public synchronized void execute(final Runnable r) {
//將Runnable包裝建车,并添加到隊(duì)里中扩借。
tasks.offer(new Runnable() {
public void run() {
try {
r.run();
} finally {
//執(zhí)行完畢后,會(huì)自動(dòng)調(diào)用隊(duì)里中的下一個(gè)Runnable
scheduleNext();
}
}
});
if (active == null) {
//首次調(diào)度
scheduleNext();
}
}
protected synchronized void scheduleNext() {
if ((active = tasks.poll()) != null) {
executor.execute(active);
}
}
}}
//ThreadPerTaskExecutor是Runnable的實(shí)際執(zhí)行的地方缤至。為每一個(gè)Runnable創(chuàng)建一個(gè)Thread潮罪。
class ThreadPerTaskExecutor implements Executor {
public void execute(Runnable r) {
new Thread(r).start();
}
}}
//執(zhí)行:只有任務(wù)1執(zhí)行完畢后才會(huì)執(zhí)行任務(wù)2
Executor executor = new SerialExecutor(new ThreadPerTaskExecutor());
executor.execute(new Runnable(){
public void run() {
//任務(wù)1...
}
});
executor.execute(new Runnable(){
public void run() {
//任務(wù)2...
}
});
在這個(gè)例子中,SerialExecutor用于Runnable的調(diào)度领斥,它將Runnable進(jìn)行簡(jiǎn)單的包裝嫉到,并保存到隊(duì)里中。經(jīng)過包裝后月洛,不僅會(huì)執(zhí)行原始提交Runnable的代碼何恶,并且會(huì)執(zhí)行結(jié)束后調(diào)用隊(duì)列中的下一個(gè)Runnable。就這樣,SerialExecutor實(shí)現(xiàn)了Runnable的順序執(zhí)行嚼黔。而ThreadPerTaskExecutor類則是用來控制Runnable執(zhí)行細(xì)節(jié)的地方细层,在這里,我們簡(jiǎn)單的為每個(gè)Runnable創(chuàng)建了新的Thread唬涧,在實(shí)際應(yīng)用中疫赎,你可以使用線程池來復(fù)用Thread。
總結(jié)
Executor被創(chuàng)造的原因:將任務(wù)的執(zhí)行碎节、任務(wù)的調(diào)度捧搞、任務(wù)的執(zhí)行細(xì)節(jié)進(jìn)行解耦。
- 任務(wù)是如何調(diào)度的,你是想一個(gè)個(gè)的順序執(zhí)行实牡,還是想多個(gè)任務(wù)一起并發(fā)并能自由的控制并發(fā)數(shù)量上線陌僵,都可以通過自己實(shí)現(xiàn)Executor接口進(jìn)行定制。你只需要將任務(wù)通過Executor#execute(Runnable)方法提交進(jìn)來就可以了创坞,具體的調(diào)度方案碗短,Runnable不需要關(guān)注。這樣就實(shí)現(xiàn)了任務(wù)執(zhí)行和調(diào)度的解耦题涨。
- 對(duì)于任務(wù)的執(zhí)行細(xì)節(jié)偎谁,你是想為每個(gè)任務(wù)都創(chuàng)建新的線程,還是想復(fù)用已有的線程纲堵,也可以通過實(shí)現(xiàn)Executor接口進(jìn)行定制巡雨。同樣,你只需要將任務(wù)通過Executor#execute(Runnable)方法提交進(jìn)來就可以了席函,具體的執(zhí)行細(xì)節(jié)铐望,Runnable不需要關(guān)注。這樣就實(shí)現(xiàn)了任務(wù)執(zhí)行和執(zhí)行細(xì)節(jié)的解耦茂附。
設(shè)計(jì)模式的使用:中介者模式
在這里正蛙,使用了設(shè)計(jì)模式中的中介者模式,下面是中介者模式的定義和UML類圖营曼。
中介者模式(Mediator Pattern):定義一個(gè)中介對(duì)象來封裝系列對(duì)象之間的交互乒验。中介者使各個(gè)對(duì)象不需要顯示地相互引用,從而使其耦合性松散蒂阱,而且可以獨(dú)立地改變他們之間的交互锻全。
- 抽象中介者(Mediator)角色:抽象中介者角色定義統(tǒng)一的接口用于各同事角色之間的通信。
- 具體中介者(Concrete Mediator)角色:具體中介者角色通過協(xié)調(diào)各同事角色實(shí)現(xiàn)協(xié)作行為录煤。為此它要知道并引用各個(gè)同事角色鳄厌。
- 同事(Colleague)角色:每一個(gè)同事角色都知道對(duì)應(yīng)的具體中介者角色,而且與其他的同事角色通信的時(shí)候妈踊,一定要通過中介者角色協(xié)作部翘。——《設(shè)計(jì)模式》
UML類圖:
其中Executor就是一個(gè)抽象中介者角色响委,Executor的具體實(shí)現(xiàn)就是一個(gè)具體中介者角色新思,每個(gè)Runnable就相當(dāng)于同事角色,Executor的execute(Runnable...)方法即是各個(gè)Runnable之間通信的接口赘风,每個(gè)Runnable都被提交到Executor的具體實(shí)現(xiàn)類中夹囚,由Executor的具體實(shí)現(xiàn)類來協(xié)調(diào)各Runnable之間的實(shí)現(xiàn)協(xié)作行為。