Java 线程池 从入门到深入

类图

该类图也是从左到右按入门到深入绘制的。

Java 线程池.png

入门

创建线程池实例

使用java.util.concurrent.Executors工具类创建线程池。
Executors工具类提供如下静态方法来创建多种类型的线程池实例:

public static ExecutorService newSingleThreadExecutor()
public static ExecutorService newFixedThreadPool(int nThreads) 
public static ExecutorService newCachedThreadPool() 
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
public static ScheduledExecutorService newSingleThreadScheduledExecutor()
public static ExecutorService newWorkStealingPool()

Executors抽象的常用的线程池类型

  • 单线程线程池
public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
  • 固定工作线程数量大小线程池
public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
  • 可缓存线程池
public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
  • 定时周期性线程池
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
  • 单线程定时周期性线程池
public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }
  • 窃取工作的线程池
public static ExecutorService newWorkStealingPool() {
        return new ForkJoinPool
            (Runtime.getRuntime().availableProcessors(),
             ForkJoinPool.defaultForkJoinWorkerThreadFactory,
             null, true);
    }

通过线程池运行线程

可以提交单个、多个线程。

提交单个线程

  • java.util.concurrent.Executor#execute 无返回
  • java.util.concurrent.ExecutorService#submit(java.util.concurrent.Callable<T>) 返回Future

提交多个线程

  • java.util.concurrent.ExecutorService#invokeAll(java.util.Collection<? extends java.util.concurrent.Callable<T>>)
  • java.util.concurrent.ExecutorService#invokeAny(java.util.Collection<? extends java.util.concurrent.Callable<T>>)

停止线程池

  • void shutdown() 启动有序关闭,在此过程中执行先前提交的任务,但不接受新的任务。之后提交的任务将抛出java.util.concurrent.RejectedExecutionException异常。
  • List<Runnable> shutdownNow(); 尝试停止所有正在执行的任务,停止对正在等待的任务的处理,并返回正在等待执行的任务的列表。、

线程池结束状态

  • boolean isShutdown(); 如果这个执行器已经被关闭,返回true
  • boolean isTerminated(); 如果关闭后所有任务都已完成,则返回true。注意,除非首先调用shutdown或shutdownNow,否则isTerminated永远不会为真。

Executors创建线程池不足

是否真的存在阿里巴巴Java开发手册中存在的问题有待考量。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容