Java并发编程-Executor框架(转)
本文转自http://blog.csdn.net/chenchaofuck1/article/details/51606224 感谢作者
我们在传统多线程编程创建线程时,常常是创建一些Runnable对象,然后创建对应的Thread对象执行它们,但是如果程序需要并发执行大量的任务时,需要为每个任务都创建一个Thread,进行管理,这将会影响程序的执行效率,并且创建线程过多将会使系统负载过重。
在JDK 1.5之后通过了一套Executor框架能够解决这些问题,能够分解任务的创建和执行过程。该框架包括Executor,ExecutorService,Callable等基础接口和Executors,ThreadPoolExecutor等实现类。
创建线程池:
Executor框架的最核心的类是ThreadPoolExecutor,它是线程池的实现类,创建ThreadPoolExecutor一般使用Executors工厂模式创建,Executors类提供了一系列工厂方法用于创先线程池:
public static ExecutorService newFixedThreadPool(int nThreads)创建固定数目线程的线程池,表示最多创建nThreads个线程,如果传入的任务数大于nThreads时不会创建新的线程,而是阻塞等待有空闲线程执行。
public static ExecutorService newCachedThreadPool()创建一个可缓存的线程池,调用execute将重用以前构造的线程(如果线程可用)。如果现有线程没有可用的,则创建一个新线程并添加到池中。终止并从缓存中移除那些已有 60秒钟未被使用的线程。
public static ExecutorService newSingleThreadExecutor()创建一个单线程的Executor。
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 创建一个支持定时及周期性的任务执行的线程池,多数情况下可用来替代Timer类。
常用方法:
shutDown():关闭执行器,在关闭前允许执行以前提交的任务执行器执行完。调用shutDown()后,再发送任务给Executor将会被拒绝,抛出RejectExecutionException异常。
shutdownNow() :立即关闭执行器,阻止等待任务启动,并试图停止当前正在执行的任务。返回等待执行的任务列表。
isShutdown():调用shutDown()后,返回true。
isTerminated():调用shutDown()后,并且执行器完成了关闭过程,返回true。
getPoolSize():获取当前线程池的线程数量
getActiveCount():获取线程池中活动线程的数量
getCompleteCount():获取线程池中完成的任务数。
import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; public class ExecutorTest { public static void main(String[] args){ ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++){ executor.execute(new task()); } executor.shutdown(); while(!executor.isTerminated()){ System.out.printf("Pool size:%d,Active count:%d,Completed Task:%d\n",executor.getPoolSize(),executor.getActiveCount(),executor.getCompletedTaskCount()); } } } class task implements Runnable{ public void run() { System.out.println(Thread.currentThread().getName() + " is called"); try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
结果:
pool-1-thread-2 is called
pool-1-thread-4 is called
pool-1-thread-5 is called
pool-1-thread-3 is called
pool-1-thread-1 is called
Pool size:5,Active count:5,Completed Task:0
Pool size:5,Active count:5,Completed Task:0
Pool size:5,Active count:5,Completed Task:0
Pool size:5,Active count:5,Completed Task:0
Java并发编程-Executor框架(转)的更多相关文章
- Java 并发编程——Executor框架和线程池原理
Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...
- (转)java并发编程--Executor框架
本文转自https://www.cnblogs.com/MOBIN/p/5436482.html java并发编程--Executor框架 只要用到线程,就可以使用executor.,在开发中如果需要 ...
- Java 并发编程——Executor框架和线程池原理
Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...
- java并发编程-Executor框架
Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService,Completion ...
- Java 并发编程 Executor 框架
本文部分摘自<Java 并发编程的艺术> Excutor 框架 1. 两级调度模型 在 HotSpot VM 的线程模型中,Java 线程被一对一映射为本地操作系统线程.在上层,Java ...
- Java并发编程-Executor框架集
Executor框架集对线程调度进行了封装,将任务提交和任务执行解耦. 它提供了线程生命周期调度的所有方法,大大简化了线程调度和同步的门槛. Executor框架集的核心类图如下: 从上往下,可以很清 ...
- java并发编程--Executor框架(一)
摘要: Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程 ...
- java并发编程-Executor框架 + Callable + Future
from: https://www.cnblogs.com/shipengzhi/articles/2067154.html import java.util.concurrent.*; public ...
- java 并发编程 Executor框架
http://blog.csdn.net/chenchaofuck1/article/details/51606224 demo package executor; import java.util. ...
随机推荐
- MySQLfailover错误一则
由于公司现有主库要转移到新的主库上,所以,我打算利用MySQLfailover工具的故障转移. 1.开发把程序账号转移到新主库上 2.停止现有主库,使之进行故障转移,转移期间会自动锁表,保持数据一致性 ...
- 【Linux】用户与权限
追加用户组 groupadd 用户组名 追加新用户 useradd -d 指定用户目录 -s 指定用户使用shell -g 指定用户组 -p 指定用户密码 用户名 更改用户 添加用户到其他组 use ...
- python中强大的testdata库自动生成测试所需要的数据
testdata是用于生成测试数据的一个安装包,它不仅提供DictFactory类来生成数据,还提供特定的扩展功能.每个Factory实例均可用于生成用户所需要的特定个数的数据,这将使我们更好地统计分 ...
- RESTful API批量操作的实现
要解决的问题 RESTful API对于批量操作存在一定的缺陷.例如资源的删除接口: DELETE /api/resourse/<id>/ 如果我们要删除100条数据怎么搞?难道要调用10 ...
- opencv中ptr的使用
#include <QCoreApplication> #include<stdio.h> #include<opencv2/highgui/highgui.hpp> ...
- LeetCode(90) Subsets II
题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- Educational Codeforces Round 31- D. Boxes And Balls
D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...
- MySQL外键设置 级联删除
. cascade方式在父表上update/delete记录时,同步update/delete掉子表的匹配记录 . set null方式在父表上update/delete记录时,将子表上匹配记录的列设 ...
- centos 7安装libreoffice
centos 7安装libreoffice Centos下的LibreOffice安装: 关键字: LibreOffice , sdk ,汉化 ,解压 安装Centos7之后,系统安装了libreo ...
- TOJ 2703: Cow Digit Game
2703: Cow Digit Game Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByte Total Submit ...