博客
关于我
线程池之SingleThreadPool学习
阅读量:310 次
发布时间:2019-03-03

本文共 571 字,大约阅读时间需要 1 分钟。

简介

SingleThreadPool是使用单个工作线程的线程池。

创建方法

public static ExecutorService newSingleThreadExecutor() {           return new FinalizableDelegatedExecutorService            (new ThreadPoolExecutor(1, 1,                                    0L, TimeUnit.MILLISECONDS,                                    new LinkedBlockingQueue
())); }

corePoolSize和maximumPoolSize都是1,即SingleThreadPool只有一个核心线程。其他参数都和FixedThreadPool一样。

execute方法执行

在这里插入图片描述执行execute方法时,若当前运行的线程数未达到核心线程数(没有正在运行的线程),就创建一个新线程来处理任务;如果当前有运行的线程,就把任务添加到阻塞队列LinkedBlockingQueue。SingleThreadPool能够确保所有的任务都在一个线程中按照顺序逐一执行。

转载地址:http://xnbq.baihongyu.com/

你可能感兴趣的文章