Executors线程池
2017-03-09 16:12
417 查看
1. Executors简介
Executor的优点是把任务的提交和执行解耦。简单的说就是,只需把需要执行Task描述清楚,然后提交即可,这个Task是怎么被执行的,被谁执行的,什么时候执行的,都不用关心了。
具体点说就是,提交一个Callable/Runnable对象给ExecutorService,然后ExecutorService调度线程去执行你提交的task,分两种情况:
1、如果你不关心task的执行结果,直接提交Runnable对象给ExecutorService即可。
2、如果你需要得到task执行的结果,就submitCallable/Runnable,然后你将得到一个Future对象,调用Future对象的get方法等待执行结果就好了。
Executors提供四种线程池 :
newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数。
newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
2. 示例代码
public static void testSingleThreadPool() {ExecutorService executors = Executors.newSingleThreadExecutor();
for (int i = 0; i < 20; i++) {
System.out.println("submit tast #" + i);
executors.execute(new Runnable() {
@Override
public void run() {
System.out.println("only one thread to execute all the task, thread name : " + Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
}
});
}
executors.shutdown();
}
public static void testScheduledThreadPool() {
ScheduledExecutorService scheduledExecutors = Executors.newScheduledThreadPool(5);
System.out.println("delay 5 seconds to execute the task AAAAA.");
scheduledExecutors.schedule(new Runnable() {
@Override
public void run() {
System.out.println("execute task AAAAA completed.");
}
}, 5, TimeUnit.SECONDS);
System.out.println("delay 3 seconds to execute task BBBBB.");
scheduledExecutors.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println("execute task BBBBB every 5 seconds.");
}
}, 3, 5, TimeUnit.SECONDS);
}
public static void testCachedThreadPool() {
ExecutorService executors = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
System.out.println("submit tast #" + i);
executors.execute(new Runnable() {
@Override
public void run() {
System.out.println("start execute thread :" + Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("execute thread :" + Thread.currentThread().getName() + " completed.");
}
});
}
try {
Thread.sleep(6000);
} catch (InterruptedException e1) {
}
System.out.println("回收空闲线程来执行此任务");
executors.execute(new Runnable() {
@Override
public void run() {
System.out.println("start execute thread :" + Thread.currentThread().getName());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("execute thread :" + Thread.currentThread().getName() + " completed.");
}
});
executors.shutdown();
}
public static void testFixedThreadPool() {
// only 3 threads
ExecutorService executors = Executors.newFixedThreadPool(3);
// but submit 20 task to execute
for (int i = 0; i < 20; i++) {
System.out.println("submit tast #" + i);
executors.execute(new Runnable() {
@Override
public void run() {
System.out.println("start execute thread :" + Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
System.out.println("execute thread :" + Thread.currentThread().getName() + " completed.");
}
});
}
executors.shutdown();
}
相关文章推荐
- Executors的四种线程池
- Java 多线程编程之九:使用 Executors 和 ThreadPoolExecutor 实现的 Java 线程池的例子
- 类 Executors创建线程池用法
- Executors 与线程池
- 我理解的Java并发基础(七):线程池和Executors工具类
- 黑马程序员--Java Executors(线程池)
- Executors创建的4种线程池的使用
- 线程池 Executors
- Executors提供的四种线程池和自定义线程池
- Android(java)的线程池:ExecutorService和Executors简单介绍
- Java 自带的线程池Executors.newFixedThreadPool
- JDK 源码解析 —— Executors ExecutorService ThreadPoolExecutor 线程池
- Executors创建线程池的四中方式
- Executors提供的四种线程池
- Executors.newFixedThreadPool(NTHREADS)线程池数量设置多少合适?
- Java通过Executors提供四种线程池
- 用Executors来创建三种java的自带线程池
- 线程池实例:使用Executors和ThreadPoolExecutor
- [Java并发编程(二)] 线程池 FixedThreadPool、CachedThreadPool、ForkJoinPool?为后台任务选择合适的 Java executors
- 线程池系列一:线程池作用及Executors方法讲解