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 
相关文章
相关标签/搜索