博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 多线程(2)-Executor
阅读量:6856 次
发布时间:2019-06-26

本文共 2971 字,大约阅读时间需要 9 分钟。

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的例子:

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

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.

pool-1-thread-2 Start. Command = 1pool-1-thread-4 Start. Command = 3pool-1-thread-1 Start. Command = 0pool-1-thread-3 Start. Command = 2pool-1-thread-5 Start. Command = 4pool-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 = 8pool-1-thread-2 End.pool-1-thread-2 Start. Command = 9pool-1-thread-1 Start. Command = 7pool-1-thread-5 Start. Command = 6pool-1-thread-4 Start. Command = 5pool-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

  

  

转载地址:http://rxyyl.baihongyu.com/

你可能感兴趣的文章
Speedy —— 京东推出的 Docker 镜像存储系统
查看>>
盘点当下最流行的 Java 工具
查看>>
《NX-OS与Cisco Nexus交换技术:下一代数据中心架构(第2版)》一2.3 PVLAN
查看>>
6月23日云栖精选夜读:重磅!阿里妈妈首次公开自研CTR预估核心算法MLR
查看>>
《Hadoop大数据分析与挖掘实战》——1.4节数据挖掘建模过程
查看>>
重拾开始菜单的 Windows 9
查看>>
2016 最常见密码排行榜出炉 “123456”稳稳上榜
查看>>
《OpenStack云计算实战手册(第2版)》一1.9 配置服务的租户和服务的用户
查看>>
Python控制多进程与多线程并发数
查看>>
《容器技术系列》一2.3 Docker命令执行
查看>>
《HTML5游戏编程核心技术与实战》一2.4 坐标变换
查看>>
《互联网产品设计》一2.5 写代码,让产品可以使用
查看>>
大数据的真正价值在哪里?
查看>>
最新版AlphaGo(Master)的60胜预示着人类将是一种过时的算法?
查看>>
美使馆9年pm2.5数据分析:雾霾到底是不是加重了?
查看>>
《嵌入式Linux开发实用教程》——4.3 块设备驱动
查看>>
《Maven官方文档》POM文件(二)
查看>>
Apache Storm 官方文档 —— 配置开发环境
查看>>
企业IT架构转型之道:阿里巴巴中台战略思想与架构实战. 2.5 为真正发挥大数据威力做好储备...
查看>>
如何通过 MySQL 的二进制日志恢复数据库数据
查看>>