本文共 3383 字,大约阅读时间需要 11 分钟。
* ThreadPoolExecutor(线程池)
* 理论: * 1、概念:线程池做的工作主要是控制运行的线程的数量,处理过程中将任务加入队列,然后在线程创建后启动这些任务。 * 2、运作流程:当前线程数 < 核心线程,创建新线程,当前线程数 > 核心 < 最大,加入队列,当前队列满,创建新线程。 * 超过最大线程并且队列满则抛弃。一、ThreadPoolExecutor的参数解释:
* 1、核心参数:
* corePoolSize: 常驻核心线程数 * maximumPoolSize: 最大线程数 * workQueue: 任务等待队列 * 常见任务队列: * 1) SynchronousQueue (直接提交) * 2) LinkedBlockingQueue (无参 --- 无界队列) * 3) ArrayBlockingQueue (有界队列) * 2、其他参数 * keepAliveTime:线程空闲多少时间销毁,即大于core线程时,多余的线程空闲时销毁,线程池数量降至core线程数量。 * unit: 时间单位。 * RejectedExecutionHandler:任务等待队列满了以后的拒绝策略 * AbortPolicy (默认): 抛出RejectedException异常 * CallerRunPolicy:不丢弃,但是采用一些策略减缓新任务的提交速度 * DiscardOldestPolicy:丢弃等待最长时间的 * DiscardPolicy:丢弃任务public void base1() {// public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,// TimeUnit unit, BlockingQueueworkQueue, RejectedExecutionHandler handler) new ThreadPoolExecutor(testcorePoolSize, testcorePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue ()); }
* 二、通过Executors创建线程池的常用方法
* 3) newSingleThreadExecutor 创建1个核心线程和max线程,任务队列为无界队列。
* 2) newFixedThreadPool 创建指定个数core线程和max线程,任务队列为无界队列。 * 1) newCachedThreadPool 创建0个核心线程,最大线程Integer.max,队列为SynchronousQueue。 * 4) newScheduledThreadPool 创建指定个数核心线程,最大线程,任务队列DelayedWorkQueue。 * 上面构造存在问题:newScheduledThreadPool和newCachedThreadPool指定了最大线程数为MAX_VALUE,可能会创建大量的线程,从而导致OOM。 * newFixedThreadPool和newSingleThreadExecutor的队列是无界的,可能堆积大量任务,从而导致OOM。public void base2() { ExecutorService t1 = Executors.newCachedThreadPool(); new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue()); ExecutorService t2 = Executors.newFixedThreadPool(testcorePoolSize); new ThreadPoolExecutor(testcorePoolSize, testcorePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue ()); ExecutorService t3 = Executors.newSingleThreadExecutor(); new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue ()); ScheduledExecutorService t4 = Executors.newScheduledThreadPool(testcorePoolSize);// super(testcorePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue(); }
* 三、使用案例
* 1、newCachedThreadPool 会启动20个线程进行执行。 * 2、newFixedThreadPool 会启动10个线程执行,另外10个任务先加入等待队列,10个任务执行完,再执行下10个,线程数一直是10个。 * 3、newSingleThreadExecutor 会启动1个线程执行, 另外任务加入等待队列。 * 4、自定义: 见下面注释。public static void main(String[] args) { //ExecutorService threadPool = Executors.newCachedThreadPool(); // ExecutorService threadPool = Executors.newFixedThreadPool(10); // ExecutorService threadPool = Executors.newSingleThreadExecutor(); // 自定义线程池解释:core 线程2个,max线程5个,队列3个, // 预期流程是:当启动 线程少于core线程,启动core2个,当等于core少于max,加入队列,队列装了3个,装不下, // 再启动3个线程至max数量,此时5个线程执行任务,3个任务等待,其他的由于拒绝策略是抛弃,因此执行了8个任务。 ExecutorService threadPool = new ThreadPoolExecutor( 2, 5, 1L, TimeUnit.SECONDS, new LinkedBlockingDeque(3), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy() ); try { for (int i = 1; i <= 20; i++) { threadPool.execute(() -> { try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "\t 来办理业务"); }); } } catch (Exception e) { e.printStackTrace(); } finally { threadPool.shutdown(); } }
转载地址:http://keuni.baihongyu.com/