廖雪峰Java11多线程编程-3高级concurrent包-6ExecutorService
1. 线程池
Java语言内置多线程支持:
- 创建线程需要操作系统资源(线程资源,栈空间)
- 频繁创建和销毁线程需要消耗大量时间
假设我们有大量的小任务,可以让它排队执行,然后在一个线程池里有少量的线程来执行大量的任务。
使用线程池来复用线程,可以非常高效的执行大量小任务。

线程池:
- 线程池维护若干个线程,处于等待状态
- 如果有新任务,就分配一个空闲线程执行
- 如果所有线程都处于忙碌状态,新任务放入队列等待
2. ExecutorService
JDK提供了ExecutorService接口表示线程池:
ExecutorService executor = Executors.newFixedThreadPool(4); //固定大小的线程池
executor.submit(task1); //提交任务到线程池
executor.submit(task2);
executor.submit(task3)
常用的ExecutorService:
- FixedThreadPool:线程数固定
- CachedThreadPool:线程数根据任务动态调整
- SingleThreadExecutor:仅单线程执行
2.1 FixedThreadPool示例
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class PrintTask implements Runnable{
String name;
public PrintTask(String name){
this.name = name;
}
public void run(){
for(int i=0;i<3;i++){
System.out.println(i+" Hello,"+name+"!");
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
}
}
}
public class ThreadPool {
public static void main(String[] args) throws InterruptedException{
ExecutorService executor = Executors.newFixedThreadPool(3); //指定线程池大小为3,提供了4个任务,会有1个任务等待有空闲线程后执行。
executor.submit(new PrintTask("Bob"));
executor.submit(new PrintTask("Alice"));
executor.submit(new PrintTask("Tim"));
executor.submit(new PrintTask("Robot"));
Thread.sleep(10000);
executor.shutdown(); //结束线程池
}
}

### 2.2 SingleThreadExecutor示例
```#java
//单个线程,所有的任务将串行执行
ExecutorService executor = Executors.newSingleThreadExecutor();
```

### 2.3 CachedThreadPool示例
```#java
//动态调整的线程池。由于CachedThreadPool会根据我们的任务,动态的调整线程的数量,所以这个任务提交后,线程池会立刻创建4个线程来执行它。
ExecutorService executor = Executors.newCachedThreadPool();
```

### 2.4 动态线程池指定最大线程数量
如果我们想要限制动态线程池中线程的上限,例如最多10个线程,这个时候,CachedThreadPool就不能够满足这个要求。
查看newCachedThreadPool源码,发现其实现的是ThreadPoolExecutor的构造方法,
```#java
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(
0, //初始化线程池的大小
Integer.MAX_VALUE, //线程池的最大值
60L,
TimeUnit.SECONDS,
new SynchronousQueue());
}
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue workQueue )
{
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler);
}
```
示例
```#java
//设置最大数量为10的动态线程池
ExecutorService executor = new ThreadPoolExecutor(0, 10, 60L, TimeUnit.SECONDS, new SynchronousQueue());
```
3. ScheduledThreadPool
JDK还提供了ScheduledThreadPool,使一个任务可以定期反复执行。
执行模式:
- Fixed Rate:在固定的间隔,任务就会执行。例如每隔3秒任务就会启动,而不管这个任务已执行了多长时间、是否结束
- Fixed Delay:当任务执行完毕以后,等待1秒钟再继续执行。无论任务执行多久,只有在任务结束以后,等待1秒钟才会开始执行下一次的任务。

注意:ScheduledThreadPool不会自动停止,需要手动强制结束。
3.1示例
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
class HelloTask implements Runnable{
String name;
public HelloTask(String name){
this.name = name;
}
public void run(){
System.out.println("Hello,"+name+" ! It is "+LocalTime.now());
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
System.out.println("Goodbye, "+name+"! It is "+LocalTime.now());
}
}
public class SchedulePool {
public static void main(String[] args) throws Exception{
ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
executor.scheduleAtFixedRate(new HelloTask("Bob"),2,5,TimeUnit.SECONDS); //2秒以后开始执行,每5秒就执行这个任务
executor.scheduleWithFixedDelay(new HelloTask("Alice"),2,5,TimeUnit.SECONDS); //2秒以后开始执行,执行结束等待5秒再执行
}
}

Bob的执行频率比Alice高的多,任务开始的时间差也越来越大
问题:
1.FixedRate模式下,如果任务执行时间过长,后续任务会不会并发执行?

不会
```#java
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
class HelloTask implements Runnable{
String name;
public HelloTask(String name){
this.name = name;
}
public void run(){
System.out.println("Hello,"+name+" ! It is "+LocalTime.now());
try{
Thread.sleep(10000);
}catch (InterruptedException e){}
System.out.println("Goodbye, "+name+"! It is "+LocalTime.now());
}
}
public class SchedulePool {
public static void main(String[] args) throws Exception{
ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
executor.scheduleAtFixedRate(new HelloTask("Bob"),2,1,TimeUnit.SECONDS);
}
}
<img src="https://img2018.cnblogs.com/blog/1418970/201906/1418970-20190613214419984-1491212580.png" width="500" />
<font color=#FF0000><strong>2.如果任务抛出了异常,后续任务是否继续执行?</strong></font>
<font color=#458B00>不会</font>
```#java
import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
class HelloTask implements Runnable{
String name;
int count;
public HelloTask(String name,int count){
this.name = name;
this.count = count;
}
public void run(){
System.out.println("Hello,"+name+" ! It is "+LocalTime.now()+" "+count);
try{
if(count == 3){
throw new RuntimeException("我是故意的");
}
Thread.sleep(1000);
}catch (InterruptedException e){}
System.out.println("Goodbye, "+name+"! It is "+LocalTime.now());
count++;
}
}
public class SchedulePool {
public static void main(String[] args) throws Exception{
ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);
executor.scheduleAtFixedRate(new HelloTask("Bob",0),2,5,TimeUnit.SECONDS);
}
}

4. java.util.Timer
jdk还提供了java.util.Timer类,这个类也可以定期执行一个任务:
- 一个Timer对应一个Thread,只能定期执行一个任务。如果要执行多个定时任务,就必须要启动多个Timer。
- 必须在主线程结束时跳用Timer.cancel()
而一个ScheduledPool就可以调度多个任务,所以完全可以用新的Scheduled取代Timer类。
5. 总结:
- JDK提供了ExecutorService实现了线程池功能
- 线程池内部维护一组线程,可以搞笑执行大量小任务
- Executors提供了静态方法创建不同类型的ExecutorService
- 必须调用shutdown()关闭ExecutorService
- ScheduledThreadPool可以定期调度多个任务
廖雪峰Java11多线程编程-3高级concurrent包-6ExecutorService的更多相关文章
- 廖雪峰Java11多线程编程-3高级concurrent包-5Atomic
Atomic java.util.concurrent.atomic提供了一组原子类型操作: 如AtomicInteger提供了 int addAndGet(int delta) int increm ...
- 廖雪峰Java11多线程编程-3高级concurrent包-4Concurrent集合
Concurrent 用ReentrantLock+Condition实现Blocking Queue. Blocking Queue:当一个线程调用getTask()时,该方法内部可能让给线程进入等 ...
- 廖雪峰Java11多线程编程-3高级concurrent包-1ReentrantLock
线程同步: 是因为多线程读写竞争资源需要同步 Java语言提供了synchronized/wait/notify来实现同步 编写多线程同步很困难 所以Java提供了java.util.concurre ...
- 廖雪峰Java11多线程编程-3高级concurrent包-9Fork_Join
线程池可以高效执行大量小任务: Fork/Join线程池可以执行一种特殊的任务: 把一个大任务拆成多个小任务并行执行 Fork/Join是在JDK 1.7引入的 示例:计算一个大数组的和 Fork/J ...
- 廖雪峰Java11多线程编程-3高级concurrent包-8CompletableFuture
使用Future可以获得异步执行结果 Future<String> future = executor.submit(task); String result = future.get() ...
- 廖雪峰Java11多线程编程-3高级concurrent包-7Future
JDK提供了ExecutorService接口表示线程池,可以通过submit提交一个任务. ExecutorService executor = Executors.newFixedThreadPo ...
- 廖雪峰Java11多线程编程-3高级concurrent包-3Condition
Condition实现等待和唤醒线程 java.util.locks.ReentrantLock用于替代synchronized加锁 synchronized可以使用wait和notify实现在条件不 ...
- 廖雪峰Java11多线程编程-3高级concurrent包-2ReadWriteLock
ReentrantLock保证单一线程执行 ReentrantLock保证了只有一个线程可以执行临界区代码: 临界区代码:任何时候只有1个线程可以执行的代码块. 临界区指的是一个访问共用资源(例如:共 ...
- 廖雪峰Java11多线程编程-2线程同步-3死锁
1.线程锁可以嵌套 在多线程编程中,要执行synchronized块: 必须首先获得指定对象的锁 Java的线程锁是可重入的锁.对同一个对象,同一个线程,可以多次获取他的锁,即同一把锁可以嵌套.如以下 ...
随机推荐
- ionic:安装
ylbtech-ionic:安装 1.返回顶部 1. ionic 安装 本站实例采用了ionic v1.3.2 版本,使用的 CDN 库地址: <link href="https:// ...
- day26-多态、封装、反射
#!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...
- PAT_A1020#Tree Traversals
Source: PAT A1020 Tree Traversals (25 分) Description: Suppose that all the keys in a binary tree are ...
- 转载:jQuery 获取屏幕高度、宽度
做手机Web开发做浏览器兼容用到了,所以在网上找了些汇总下. alert($(window).height()); //浏览器当前窗口可视区域高度 alert($(document).height() ...
- linux 平台安装JDK环境
1.检查一下系统中的jdk版本 [root@localhost software]# java -version 2.检测jdk安装包 [root@localhost software]# rpm - ...
- NEERC 1999 Advertisement /// oj22646
题目大意: 输入k,n :k为每位慢跑者最少应看到的广告牌数 接下来n行 描述第 i 位慢跑者的途径路段 输出需要设立的广告牌数 接下来每行为设立地点 Sample Input 5 101 1020 ...
- leetcode-50-pow()
题目描述: 方法一: class Solution: def myPow(self, x: float, n: int) -> float: if n<0: x = 1/x return ...
- 校园商铺-2项目设计和框架搭建-10验证controller
1.新建package:com.csj2018.o2o.web.superadmin 2.建立AreaController.java package com.csj2018.o2o.web.super ...
- DOM——获取页面元素
获取页面元素 为什么要获取页面元素 例如:我们想要操作页面上的某部分(显示/隐藏,动画),需要先获取到该部分对应的元素,才进行后续操作 根据id获取元素 var div = document.getE ...
- 云HBase备份恢复,为云HBase数据安全保驾护航
摘要: 介绍了阿里云HBase自研备份恢复功能的基本背景以及基本原理架构和基本使用方法. 云HBase发布备份恢复功能,为用户数据保驾护航.对大多数公司来说数据的安全性以及可靠性是非常重要的,如何 ...