线程池以及四种常见线程池

线程池以及四种常见线程池

public ThreadPoolExecutor(int corePoolSize,//核心线程池大小,核心线程将会尽可能地一直活着
                          int maximumPoolSize,//线程池最大数量包括核心线程数量
                          long keepAliveTime,//非核心线程最长存活时间
                          TimeUnit unit,//keepAliveTime的单位
                          BlockingQueue<Runnable> workQueue,//等待线程的队列
                          ThreadFactory threadFactory,//线程工程
                          RejectedExecutionHandler handler)//线程拒绝执行回调

四种常见的线程池:

  • Executors.newCachedThreadPool()

    new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>())
    
  • Executors.newFixedThreadPool(int nThreads)

    new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>())
    
  • Executors.newScheduledThreadPool(int nCorepoolSize)

    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
    
    //ScheduledThreadPoolExecutor():
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue());
    }
    

  • Executors.newSingleThreadPool()

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

posted @ 2018-11-27 17:30  帅不过王力宏  阅读(166)  评论(0编辑  收藏  举报