User Tools

Site Tools


java-concurrent:executor
        ExecutorService executorService = Executors.newCachedThreadPool();     //一个任务创建一个线程
        ExecutorService executorService2 = Executors.newSingleThreadExecutor();  //相当于大小为 1 的 FixedThreadPool
        ExecutorService executorService3 =  Executors.newFixedThreadPool(5);  //所有任务只能使用固定大小的线程
        ExecutorService executorService4 =  Executors.newScheduledThreadPool(5);  //创建一个定长线程池,支持定时及周期性任务执行。
 

用法很简单,所以尽可能看实现

newCachedThreadPool()  => new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
newSingleThreadExecutor() => new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));
newFixedThreadPool() => new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
newScheduledThreadPool() => new ScheduledThreadPoolExecutor(corePoolSize);

本质上都是 对 ThreadPoolExecutor方法的不同方式调用而已。 他的七个参数: RejectedExecutionHandler, corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit, runnableTaskQueue, ThreadFactory

java-concurrent/executor.txt · Last modified: 2019/09/10 07:49 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: 沪ICP备12046235号-2
Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki