Java 多線程(2)-Executor

public interface Executor{
	void executor(Runnable command);
}

如上所寫,Executor其實是一個接口,他提供了惟一的接口方法executor(Runnable command)java

Executor其實是提供了一個線程池的概念, 他的優勢是實現了多線程任務的提交和執行的解耦。 經過Executor,使用者能夠不用關心任務是被哪個線程執行的,何時執行的。只須要等待線程執行完畢,並得到最後的結果就能夠了。舉個簡答的例子:多線程

咱們工做中當老大的老大(且稱做LD^2)把一個任務交給咱們老大(LD)的時候,究竟是LD本身幹,仍是轉過身來拉來一幫苦逼的兄弟加班加點幹,那LD^2是無論的。LD^2只用把人描述清楚說起給LD,而後喝着咖啡等着收LD的report便可。等LD一封郵件很是優雅框架

地報告LD^2report結果時,實際操做中是碼農A和碼農B幹了一個月,仍是碼農ABCDE加班幹了一個禮拜,大可能是不用體現的。這套機制的優勢就是LD^2找個合適的LD出來提交任務便可,接口友好有效,不用爲具體怎麼幹費神費力。ide

-----(戲(細)說Executor框架線程池任務執行全過程)this

下面是一個簡單的套用Executor的例子:spa

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.javablog

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.get

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而且提供了一個額外的方法

public interface ExecutorService extends Executor 
相關文章
相關標籤/搜索