您的位置:首页 > 其它

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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息