Skip to content

Latest commit

 

History

History
45 lines (31 loc) · 960 Bytes

thread-pool.md

File metadata and controls

45 lines (31 loc) · 960 Bytes

线程池原理与实现

使用

ExecutorService service = Executors.newCachedThreadPool();
service.execute(new Runnable() {
    @Override
    public void run() {

    }
});

Future<Integer> result = service.submit(new Callable<Integer>() {

    @Override
    public Integer call() throws Exception {
        return 1;
    }
});

executors

newCachedThreadPool()为例

newCachedThreadPool

所以问题的关键是ThreadPoolExecutor

ThreadPoolExecutor

thread-pool-executor构造函数

默认的饱和策略是AbortPolicy

基本的任务排队方法有三种

  1. 无界队列
  2. 有界队列
  3. 同步移交

扩展ThreadPoolExecutor

  1. beforeExecute
  2. afterExecute
  3. terminated