Skip to content

Latest commit

 

History

History
83 lines (66 loc) · 2.89 KB

File metadata and controls

83 lines (66 loc) · 2.89 KB

J.U.C中的实现ThreadPoolExecutor

  • 当前线程数小于corePoolSize,直接执行addWorker方法创建线程
  • offer到queue
  • queue满了,执行addWorker方法创建新的线程执行任务
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
    if (addWorker(command, true))
        return;
    c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
    int recheck = ctl.get();
    if (! isRunning(recheck) && remove(command))
        reject(command);
    else if (workerCountOf(recheck) == 0)
        addWorker(null, false);
}
else if (!addWorker(command, false))
    reject(command);
  1. Tomcat的实现StandardThreadExecutor Tomcat中ThreadPoolExecutor实现

  2. motan参考了tomcat的StandardThreadExecutormotan的实现StandardThreadExecutor

  3. 点评pigeon的实现DefaultThreadPool

  4. dubbo实现

  5. Tomcat线程池详解

几种的弊端

Executors.newCachedThreadPool();线程数最大Integer.MAX_VALUE,为不受限制,

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

Executors.newFixedThreadPool();对列大小为Integer.MAX_VALUE

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
public LinkedBlockingQueue() {
    this(Integer.MAX_VALUE);
}

Executors.newSingleThreadExecutor();

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

Executors.newScheduledThreadPool();

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
    super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
          new DelayedWorkQueue());
}