SingleThreadPool构造方法
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(
new ThreadPoolExecutor(1,
1,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
复制代码
特点: 单个后台线程(其缓冲队列无界)。单线程串行执行所有任务。
使用案例:
val pool: ExecutorService = Executors.newSingleThreadExecutor()
复制代码
txt.click {
for (i in 0 until 30) {
val runnable = Runnable {
try {
Thread.sleep(2000)
log("当前线程是:", Thread.currentThread.name)
}catch(e: Exception) {
e.printStackTrace()
}
}
pool.execute(runnable)
}
}
复制代码
转载于:https://juejin.im/post/5cc55f73f265da036207b2dc
最后
以上就是感性汽车最近收集整理的关于一步步了解线程池之单核线程池-SingleThreadPool的全部内容,更多相关一步步了解线程池之单核线程池-SingleThreadPool内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复