阿里推荐的线程使用方法 ThreadPoolExecutor
阿里推荐原因:使用线程池可以减少创建和销毁线程上所花的时间以及系统资源的开销,然后之所以不用Executors自定义线程池,用ThreadPoolExecutor是为了规范线程池的使用,还有让其他人更好懂线程池的运行规则。
先说一下关于线程的概念
任务:线程需要执行的代码,也就是Runnable
任务队列:线程满了,就任务就放入任务队列里等待,等其他任务在线程里执行完,这个线程就空出来了,任务队列就将最早来的未执行的任务放入线程执行。
核心线程:线程池一直存在的线程
最大线程数量:指的是线程池所容纳的最多的线程数量
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
第一个是核心线程数量,第二个是最大线程数量,第三个是非核心线程的闲置超时时间,超过这个时间就会被回收,第四个是线程池中的任务队列模式。
我们主要讲这个任务队列模式
1.LinkedBlockingDeque
我先上一个例子代码,再来唠嗑
public class MainActivity extends AppCompatActivity {
Button button;
ThreadPoolExecutor executor;
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_record_video);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
executor.execute(myRunnable);
executor.execute(myRunnable);
executor.execute(myRunnable);
}
});
executor = new ThreadPoolExecutor(3, 6, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());
}
}
当我们点击一次按钮
03-03 15:00:43.503 5292-5333/com.example.zth.seven I/System.out: pool-1-thread-1 run
03-03 15:00:43.505 5292-5335/com.example.zth.seven I/System.out: pool-1-thread-3 run
03-03 15:00:43.505 5292-5334/com.example.zth.seven I/System.out: pool-1-thread-2 run
连续点击两次
03-03 15:10:47.941 7114-7259/com.example.zth.seven I/System.out: pool-1-thread-1 run
03-03 15:10:47.941 7114-7260/com.example.zth.seven I/System.out: pool-1-thread-2 run
03-03 15:10:47.942 7114-7261/com.example.zth.seven I/System.out: pool-1-thread-3 run
03-03 15:10:49.942 7114-7260/com.example.zth.seven I/System.out: pool-1-thread-2 run
03-03 15:10:49.942 7114-7259/com.example.zth.seven I/System.out: pool-1-thread-1 run
03-03 15:10:49.942 7114-7261/com.example.zth.seven I/System.out: pool-1-thread-3 run
这个时候就可以看出虽然说是最大线程数量是6,但是只用核心线程,不创建新线程,如果核心线程用完了就在队列里等待,队列的大小没有限制(你可随便点,他反正后来都会执行)
但是还没完这个LinkedBlockingDeque还可以设置队列数量,如果我把队列设置为2
executor = new ThreadPoolExecutor(3, 6, 3,
TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(2));
然后连续点击2两次
03-03 15:19:39.851 8971-9013/com.example.zth.seven I/System.out: pool-1-thread-1 run
03-03 15:19:39.851 8971-9014/com.example.zth.seven I/System.out: pool-1-thread-2 run
03-03 15:19:39.851 8971-9015/com.example.zth.seven I/System.out: pool-1-thread-3 run
03-03 15:19:40.045 8971-9016/com.example.zth.seven I/System.out: pool-1-thread-4 run
03-03 15:19:41.851 8971-9013/com.example.zth.seven I/System.out: pool-1-thread-1 run
03-03 15:19:41.851 8971-9015/com.example.zth.seven I/System.out: pool-1-thread-3 run
他这个时候能够创建四个线程了,为何,首先第一次点击占了三个核心线程,然后第二次点击将前两次任务放入队列里,然后队列满了就创建一个新线程用来执行最后一个任务
如果我们连续点击三次,程序在我们点击第三次就崩溃了,因为线程数量超出最大线程数量了
总结:LinkedBlockingDeque就首先使用核心线程,核心线程用完了就把任务放入队列里等待,如果队列满了就创建新线程,注意不设置队列数量,他就默认无限大。
2.SynchronousQueue
我们还是和以前一样换掉一行代码
executor = new ThreadPoolExecutor(3, 6, 3,
TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
点击一次
03-03 15:39:31.915 12403-12446/com.example.zth.seven I/System.out: pool-1-thread-2 run
03-03 15:39:31.915 12403-12447/com.example.zth.seven I/System.out: pool-1-thread-3 run
03-03 15:39:31.915 12403-12445/com.example.zth.seven I/System.out: pool-1-thread-1 run
连续点击两次
03-03 15:39:53.204 12403-12446/com.example.zth.seven I/System.out: pool-1-thread-2 run
03-03 15:39:53.204 12403-12445/com.example.zth.seven I/System.out: pool-1-thread-1 run
03-03 15:39:53.204 12403-12447/com.example.zth.seven I/System.out: pool-1-thread-3 run
03-03 15:39:53.371 12403-12501/com.example.zth.seven I/System.out: pool-1-thread-5 run
03-03 15:39:53.371 12403-12502/com.example.zth.seven I/System.out: pool-1-thread-6 run
03-03 15:39:53.371 12403-12500/com.example.zth.seven I/System.out: pool-1-thread-4 run
可以看出来当核心线程占满时他没有将任务放入队列里去等待,而是直接创建新线程取执行任务
连续点击三次
程序崩溃,因为他创建的线程超过了最大线程数量,
总结:SynchronousQueue很单纯,不使用队列,核心线程用完了就创建新线程,
参考文章:
http://blog.csdn.net/qq_25806863/article/details/71126867
阿里推荐的线程使用方法 ThreadPoolExecutor的更多相关文章
- 从源码看JDK提供的线程池(ThreadPoolExecutor)
一丶什么是线程池 (1)博主在听到线程池三个字的时候第一个想法就是数据库连接池,回忆一下,我们在学JavaWeb的时候怎么理解数据库连接池的,数据库创建连接和关闭连接是一个比较耗费资源的事情,对于那些 ...
- 线程中断方法interrupt() 与 cancel()
(一).关于interrupt() interrupt()并不直接中断线程,而是设定一个中断标识,然后由程序进行中断检查,确定是否中断. 1. sleep() & interr ...
- 线程池之ThreadPoolExecutor详解
为什么要使用线程池 线程是一个操作系统概念.操作系统负责这个线程的创建.挂起.运行.阻塞和终结操作.而操作系统创建线程.切换线程状态.终结线程都要进行CPU调度——这是一个耗费时间和系统资源的事情. ...
- Java并发包线程池之ThreadPoolExecutor
参数详解 ExecutorService的最通用的线程池实现,ThreadPoolExecutor是一个支持通过配置一些参数达到满足不同使用场景的线程池实现,通常通过Executors的工厂方法进行配 ...
- 线程池之 ThreadPoolExecutor
线程池之 ThreadPoolExecutor + 面试题 线程池介绍 线程池(Thread Pool):把一个或多个线程通过统一的方式进行调度和重复使用的技术,避免了因为线程过多而带来使用上的开销. ...
- Android线程管理之ThreadPoolExecutor自定义线程池
前言: 上篇主要介绍了使用线程池的好处以及ExecutorService接口,然后学习了通过Executors工厂类生成满足不同需求的简单线程池,但是有时候我们需要相对复杂的线程池的时候就需要我们自己 ...
- 线程池:ThreadPoolExecutor
[ThreadPoolExecutor的使用和思考] public ThreadPoolExecutor(int corePoolSize, ...
- 从源码解读线程(Thread)和线程池(ThreadPoolExecutor)的状态
线程是比进程更加轻量级的调度执行单位,理解线程是理解并发编程的不可或缺的一部分:而生产过程中不可能永远使用裸线程,需要线程池技术,线程池是管理和调度线程的资源池.因为前不久遇到了一个关于线程状态的问题 ...
- Java并发编程:Java线程池核心ThreadPoolExecutor的使用和原理分析
目录 引出线程池 Executor框架 ThreadPoolExecutor详解 构造函数 重要的变量 线程池执行流程 任务队列workQueue 任务拒绝策略 线程池的关闭 ThreadPoolEx ...
随机推荐
- vue-cli配置
转载--https://www.cnblogs.com/caideyipi/p/8187656.html 手撕vue-cli配置文件——config篇 最近一直在研究webpack,突然想看看vu ...
- Client does not support authentication
Navicat 11 连MySQL 8.0.16 报 Client does not support authentication........ 解决: alter user 'fabu'@'%' ...
- 2018年-2019年第二学期第二周C#学习个人总结
在本学期的第二周,我们又开始了C#的学习.在星期一的C#课上时,我们学了this关键字的用法其中包括1.this访问属性2.this访问成员方法3.this访问构造方法.在this访问属性中通过thi ...
- 解决 “MoveFile”: 类型库“XXX.dll”中的标识符已经是宏;使用“rename”限定符 类型库符号与系统符号冲突问题
今天在VS工程当中引入一个组件,编译的时候出现警告, “MoveFile”: 类型库“XXX.dll”中的标识符已经是宏:使用“rename”限定符.虽然只是一个警告,但看着实在不爽,更重要的是,警告 ...
- P4116 Qtree3
思路 可以树剖可以LCT,树剖就是每个重链开一个SET维护一下黑点的深度 非常不优美 使用LCT,在splay上二分找出需要的节点即可 代码 #include <cstdio> #incl ...
- [luaj]在安卓用使用luaj
luaj与安卓 什么是luaj luaj是一个Java的一个lua语言引擎,他可以让你在java上运行Lua代码. 在安卓中使用lua干嘛 lua代码可以用来书写布局,或者一些业务逻辑. 为什么要在安 ...
- Lintcode174-Remove Nth Node From End of List-Easy
174. Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...
- 阿里云Hadoop集群DataNode连接不上NameNode
在logs日志中可以看见DataNode多次去连NameNode,但是都失败了. 经过长时间的研究百度,终于知道了原因. 原因就是安全组限制了端口的开放,所以我们只要把相应的端口打开即可.
- 从Scratch到Python:会动的小猫
大部分人提起儿童编程,就会想到Scratch,然而当儿童升入中学,学习什么语言比较合适呢?我认为,Python是未来的方向,为此我将会把一些经典的Scratch案例用Python重新实现,抛砖引玉,希 ...
- Python绘制温度变化曲线
导入必要的第三方库 from requests import get import matplotlib.pyplot as plt /usr/lib/python3/dist-packages/ma ...