concurrent并发包入门(四)之Executor

任务执行和调度的框架

img

Executor接口:顶级接口,只有一个方法,execute(Runnable command);

Executor优点:

任务提交过程与执行过程解耦,用户只需要关心如何定义好任务即可

Executors类:相当于工具类,类似Conllections,创建

  • ExecutorService
  • ScheduledExecutorService
  • ThreadFactory
  • Callable等对象

Executors线程池

1
2
3
4
newCachedThreadPool 大小不受限,当线程释放时,可重用该线程,自动终止并从缓存中移除那些已有 60 秒钟未被使用的线程
newFixedThreadPool 大小固定,无可用线程时,任务等待,直到有可用现成 如,建立5个固定线程容量:Executors.newFixedThreadPool(5)
newSingleThreadExecutor 单线程,任务依次执行
newScheduledThreadPool 创建定长线程池,支持定时及周期性任务执行

示例newCachedThreadPool

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.concurrent.*;

/**
* @ClassMame: DemoExecutor
* @Description: TODO
* @Author 宝全
* @Date 2018/10/19 17:50
* @Version 1.0
*/
public class DemoExecutor {
public static void main(String[] args) throws Exception {
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
cachedThreadPool.execute(()->{
System.out.println("newCachedThreadPool->Runnable execute:This is Runnable Thread,no The return value");
});

Future<?> submit = cachedThreadPool.submit(new CallableDemo());
System.out.println("newCachedThreadPool->Callable submit:"+submit.get());
Future<?> submit1 = cachedThreadPool.submit(new RunnableDemo());
System.out.println("newCachedThreadPool->Runnable submit:"+submit1.get()); //null
}
}

class CallableDemo implements Callable{
@Override
public Object call() throws Exception {
return "This is Callable Thread";
}
}

class RunnableDemo implements Runnable{
@Override
public void run() {
System.out.println("This is Runnable Thread");
}
}

示例newFixedThreadPool

创建可重用的固定线程数的线程池,以共享的无界队列方式运行

示例newScheduledThreadPool

1.shedule(Runnable command, long delay, TimeUnit unit);

一次性延时,后续线程将不再延时

1
2
3
4
5
6
7
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
Thread thread=new Thread(()-> {
System.out.println("开始运动"+Thread.currentThread().getName());
});
executor.execute(thread);
}

2.sheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)

执行给定的初始化延时时间initialDelay后,间隔period时间重复执行,频率优先,按照任务开始时间计算间隔

1
2
3
ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);         
Thread thread=new Thread(()-> System.out.println("开始运动"));
threadPool.scheduleAtFixedRate(thread,5,1,TimeUnit.SECONDS);

3.scheduleWithFixDelay(Runnable command, long initialDelay, long period, TimeUnit unit)

执行给定的初始化延时时间initialDelay后,间隔period时间重复执行,间隔优先,按照[本次任务结束时间]和[下次任务开始时间]计算间隔

1
2
3
ScheduledExecutorService threadPool = Executors.newScheduledThreadPool(5);         
Thread thread=new Thread(()-> System.out.println("开始运动"));
threadPool.scheduleWithFixedDelay(thread,5,1,TimeUnit.SECONDS);

示例newSingleThreadExecutor

创建单线程化的线程池,只会用唯一的工作线程来执行任务,保证所有任务按照指定的顺序(FIFO,FILO,优先级)执行

1
2
3
4
5
6
7
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 10; i++) {
Thread thread=new Thread(()-> {
System.out.println("开始运动"+Thread.currentThread().getName());
});
executor.execute(thread);
}

https://blog.csdn.net/lpjishu/article/details/53083614