您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 郑州分类信息网,免费分类信息发布

传智播客郑州校区深入解析Java线程精进之线程池进阶

2024/1/4 23:19:07发布24次查看
今天,传智播客郑州校区来教大家如何玩转线程池。首先,取出来一“条”线程,用完后再扔回去,再取出来,再扔....
对于java线程池,传智播客总结了以下特点:
1.避免大量的创建和销毁带来的性能开销
2.避免大量的线程间因互相抢占系统资源导致的阻塞现象。
3.能够对线程进行简单的管理并提供定时执行、间隔执行等功能。
另外,传智播客还总结了一些关于线程池的缺点,如下:
1.占用一定的内存空间。
2.线程越多cpu的调度开销越大。
3.程序的复杂度会上升。
其次,传智播客还总结了线程池的基本使用方法
使用线程池,肯定要对“池”进行配置,像基本的池多大啊,能容纳多少鱼,这些是需要建造前就要确定的.
int corepoolsize核心线程最大数
无论是闲置还是工作中,永远都不会销毁的线程,会一直在线程池中存在,那我们是不是永远都奈何不了它呢,当然不会,把threadpoolexecutor的allowcorethreadtimeout这个属性设置为true,超过规定时长也会销毁
int maximumpoolsize线程总数最大值
就是我们所有的线程,包括非核心的和上边讲的核心的
long keepalivetime
如果不是核心线程,超过这个参数的时间就会被干掉
timeunit unit
时间总是有单位的,就是上边keepalivetime的时间参数.
类型时枚举,这里举几种常用的,需要时候idea会提示
seconds : 秒
minutes : 分
hours : 小时
days : 天
blockingqueue workqueue
队列内容较多,我们下边单独讲
threadfactory threadfactory这个一般不用,没必要管
rejectedexecutionhandler handler
如果执行的过程中,抛异常了,我们可以用这个来指定,但是系统有默认的,因此这个也可以不用
使用线程池当然离不开executor这个接口,具体的实现类在threadpoolexecutor中,so我们简单来了解下这个类
//使用的就是这几个构造,参数我们上边已经讲过了publicclassthreadpoolexecutor extends abstractexecutorservice { .....public threadpoolexecutor(int corepoolsize,int maximumpoolsize,long keepalivetime,timeunit unit,blockingqueue workqueue);public threadpoolexecutor(int corepoolsize,int maximumpoolsize,long keepalivetime,timeunit unit,blockingqueue workqueue,threadfactory threadfactory);public threadpoolexecutor(int corepoolsize,int maximumpoolsize,long keepalivetime,timeunit unit,blockingqueue workqueue,rejectedexecutionhandler handler);public threadpoolexecutor(int corepoolsize,int maximumpoolsize,long keepalivetime,timeunit unit,blockingqueue workqueue,threadfactory threadfactory,rejectedexecutionhandler handler); ...}
接下来,传智播客还为大家整理了常用的几种线程池
线程都池都是是使用executors这个类创建出来的
cachedthreadpool()
可缓存线程池:
线程数无限制
优先使用空闲线程,如果没有才新建线程
//创建方式executorservice cachedthreadpool = executors.newcachedthreadpool();//源码中的定义publicstatic executorservice newcachedthreadpool() {returnnew threadpoolexecutor(0, integer.max_value, 60l, timeunit.seconds,new synchronousqueue());}
fixedthreadpool()
固定长度的线程池
可以控制线程的数量
如果超过则必须排队执行
//创建方式executorservice fixedthreadpool = executors.newfixedthreadpool(线程数量);//源码中的定义publicstatic executorservice newfixedthreadpool(int nthreads) {returnnew threadpoolexecutor(nthreads, nthreads, 0l, timeunit.milliseconds,new linkedblockingqueue());}
scheduledthreadpool()
对,就是它可以定时
//创建方法executorservice scheduledthreadpool = executors.newscheduledthreadpool(最大线程数);//源码中的定义publicstatic scheduledexecutorservice newscheduledthreadpool(int corepoolsize) {returnnew scheduledthreadpoolexecutor(corepoolsize);}public scheduledthreadpoolexecutor(int corepoolsize) {super(corepoolsize, integer.max_value, default_keepalive_millis, milliseconds,new delayedworkqueue());}
singlethreadexecutor()
所有任务排队执行,一次只能执行一个任务
//创建方法executorservice singlethreadpool = executors.newsinglethreadpool();//源码中的定义publicstatic executorservice newsinglethreadexecutor() {returnnew finalizabledelegatedexecutorservice (new threadpoolexecutor(1, 1, 0l, timeunit.milliseconds,new linkedblockingqueue()));
最后,传智播客为大家简单介绍下队列
队列类似我们的集合,实现了collection接口
队列中排队执行的是我们runnable对象
常用的队列有如下几种
synchronousqueue:
有任务来就执行交给空闲线程,如果没有就新建线程执行,so 线程数设置尽可能大即integer.max_value
linkedblockingqueue:
永远在核心线程内使用线程,如果超过了核心的线程就在队列中等待执行
arrayblockingqueue:
优先使用核心线程数,其次使用非核心的,如果用完了就进入队列等待
delayqueue:
想进入队列必须实现delayed接口,达到指定时间后,方可执行任务
郑州分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录