Java中四種線程池介紹

Java裏面線程池的頂級接口是Executor,但是嚴格意義上講Executor並不是一個線程池,而是一個執行線程的工具。真正的線程池接口是ExecutorService.

1.newCachedThreadPool

爲每一個任務創建一個線程,並且可以重用已有的線程,無核心線程數量,超過60s的空閒線程將會被啓用

/**無參構造方法*/
 public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }
 /**有參的構造方法,可以傳入threadFactory*/
 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>(),
                                      threadFactory);
    }
2.newFixedTbreadPool

核心線程數和最大線程數是相同的,無空閒線程,核心線程數無限時要求,可以創建固定大小的線程數量,阻塞隊列沒有大小限制


/**創建固定大小的線程數*/
public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }
 
/**創建固定大小的線程數,並可以指定threadFactory*/
 public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>(),
                                      threadFactory);

3.newScheduledThreadPool

延期和定時執行線程的線程池,固定的核心線程數,最大線程數沒有限制,空閒的線程會立即啓用


/**創建固定大小的corePool*/
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
/**查看ScheduledThreadPoolExecutor類,發現是集成的ThreadPoolExecutor*/
 public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }
/**創建固定大小的corePool,並指定threadFactory*/
 public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }
/**查看ScheduledThreadPoolExecutor類,發現是集成的ThreadPoolExecutor*/
 public ScheduledThreadPoolExecutor(int corePoolSize,
                                       ThreadFactory threadFactory) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue(), threadFactory);

4.newSingleThreadExecutor

創建只有一個的核心線程,只處理一個線程,不能處理併發


/**無參*/
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}
/**指定threadFactory*/
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
        return new DelegatedScheduledExecutorService
            (new ScheduledThreadPoolExecutor(1, threadFactory));

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章