Threads 和 Executors


Thread

Executor

Threads

Executors

Executors 是 java.util.concurrent; 包中的一个工具类,用来快速的创建线程池对象等。

创建 ExecutorService

// 创建一个核心线程数等于最大线程数的线程池,线程队列是无界的
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
&#125;

// 创建一个核心线程数等于最大线程数的线程池,线程队列是无界的,可指定工厂函数对象
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) &#123;
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(),
                                  threadFactory);
&#125;
  • 并行线程池,不太常见
// 创建并行线程池,可以使用多个队列
public static ExecutorService newWorkStealingPool(int parallelism) &#123;
    return new ForkJoinPool
        (parallelism,
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
&#125;

public static ExecutorService newWorkStealingPool() &#123;
    return new ForkJoinPool
        (Runtime.getRuntime().availableProcessors(),
         ForkJoinPool.defaultForkJoinWorkerThreadFactory,
         null, true);
&#125;
// 创建单线程,其中核心线程数和最大线程数都是 1,任务队列是无界的
public static ExecutorService newSingleThreadExecutor() &#123;
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
&#125;

// 创建单线程,其中核心线程数和最大线程数都是 1,可以自定义指定 工厂函数对象
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) &#123;
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
&#125;
// 创建缓存线程池,核心线程为 0,最大线程为 Integer.MAX_VALUE,同步队列,长度为1
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) &#123;
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
&#125;

创建 ScheduledExecutorService

// 创建单线程,即线程池核心线程为 1 
public static ScheduledExecutorService newSingleThreadScheduledExecutor() &#123;
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
&#125;

// 创建单线程,并可以指定工厂对象
public static ScheduledExecutorService newSingleThreadScheduledExecutor() &#123;
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
&#125;
// 创建指定核心线程数的任务调度线程池
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) &#123;
    return new ScheduledThreadPoolExecutor(corePoolSize);
&#125;

// 创建指定核心线程数的任务调度线程池,并且可以指定工厂函数
public static ScheduledExecutorService newScheduledThreadPool(
    int corePoolSize, ThreadFactory threadFactory) &#123;
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
&#125;

文章作者: shone
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 shone !
评论
 上一篇
Java 多线程并发摘要 Java 多线程并发摘要
参考文档 一文秒懂系列之 Java 并发编程面试题 线程和执行器Thread 和 RunnableJava 从 JDK1.0 开始执行线程,在开始一个新的线程之前,你必须指定由这个线程执行的代码,通常称为 task 。 Java 中创建
2023-02-02
下一篇 
ThreadPoolExecutor ThreadPoolExecutor
ThreadPoolExecutor,位于 java.util.concurrent; 包中,继承了 AbstractExecutorService 抽象类,间接继承了 ExecutorService 接口。 ThreadPoolExecu
2023-02-02
  目录