线程池
ThreadPoolExecutor
参数
| corePoolSize | 指定了线程池中的线程数量,它的数量决定了添加的任务是开辟新的线程去执行,还是放到workQueue任务队列中去 |
| maximumPoolSize | 指定了线程池中的最大线程数量,这个参数会根据你使用的workQueue任务队列的类型,决定线程池会开辟的最大线程数量 |
| keepAliveTime | 当线程池中空闲线程数量超过corePoolSize时,多余的线程会在多长时间内被销毁 |
| unit | keepAliveTime的单位 |
| workQueue | 任务队列,被添加到线程池中,但尚未被执行的任务 |
| threadFactory | 线程工厂,用于创建线程,一般用默认即可 |
| handler | 拒绝策略;当任务太多来不及处理时,如何拒绝任务 |
workQueue参数
| 直接提交队列 | SynchronousQueue |
提交的任务不会被保存,总是会马上提交执行. 用于执行任务的线程数量小于maximumPoolSize,则尝试创建新的进程, 如果达到maximumPoolSize设置的最大值,则根据你设置的handler执行拒绝策略 |
| 有界的任务队列 | ArrayBlockingQueue |
若有新的任务需要执行时,线程池会创建新的线程,直到创建的线程数量达到corePoolSize时,则会将新的任务加入到等待队列中。 若等待队列已满,即超过ArrayBlockingQueue初始化的容量,则继续创建线程,直到线程数量达到maximumPoolSize设置的最大线程数量, 若大于maximumPoolSize,则执行拒绝策略。 |
| 无界的任务队列 | LinkedBlockingQueue |
线程池的任务队列可以无限制的添加新的任务,而线程池创建的最大线程数量就是你corePoolSize设置的数量,若后续有新的任务加入,则直接进入队列等待。 也就是说在这种情况下maximumPoolSize这个参数是无效的 |
| 优先任务队列 | PriorityBlockingQueue | 是一个特殊的无界队列,它其中无论添加了多少个任务,线程池创建的线程数也不会超过corePoolSize的数量,只不过其他队列一般是按照先进先出的规则处理任务,可以自定义规则根据任务的优先级顺序先后执行。 |
拒绝策略
| AbortPolicy | 该策略会直接抛出异常,阻止系统正常工作 |
| CallerRunsPolicy | 如果线程池的线程数量达到上限,该策略会把任务队列中的任务放在调用者线程当中运行 |
| DiscardOledestPolicy | 该策略会丢弃任务队列中最老的一个任务,也就是当前任务队列中最先被添加进去的,马上要被执行的那个任务,并尝试再次提交 |
| DiscardPolicy | 该策略会默默丢弃无法处理的任务,不予任何处理。当然使用此策略,业务场景中需允许任务的丢失 |
例
1-优先任务队列-AbortPolicy策略
class Tp{ public void Tp1() { final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,5, 1000,TimeUnit.MILLISECONDS,new PriorityBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) { threadPoolExecutor.execute( new TpRun(i) ); } } } class TpRun implements Runnable,Comparable<TpRun>{ int index; public TpRun(int index){ this.index = index; } @Override public void run() { try { System.out.println(index+"-"+Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //当前对象和其他对象做比较,当前优先级大就返回-1,优先级小就返回1,值越小优先级越高 @Override public int compareTo(TpRun o) { return this.index>o.index?-1:1; } } ########################## 0-pool-1-thread-1 2-pool-1-thread-3 1-pool-1-thread-2 9-pool-1-thread-3 7-pool-1-thread-2 8-pool-1-thread-1 5-pool-1-thread-3 6-pool-1-thread-1 4-pool-1-thread-2 3-pool-1-thread-3
2-有界的任务队列-AbortPolicy策略
class Tp{ public void Tp1() { final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,5, 1000,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(4), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) { threadPoolExecutor.execute( new TpRun(i) ); } } } class TpRun implements Runnable{ int index; public TpRun(int index){ this.index = index; } @Override public void run() { try { System.out.println(index+"-"+Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ################# Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task TpRun@6d6f6e28 rejected from java.util.concurrent.ThreadPoolExecutor@135fbaa4[Running, pool size = 5, active threads = 5, queued tasks = 4, completed tasks = 0] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) at Tp.Tp1(MThread.java:21) at MThread.main(MThread.java:8) 0-pool-1-thread-1 1-pool-1-thread-2 2-pool-1-thread-3 7-pool-1-thread-4 8-pool-1-thread-5 3-pool-1-thread-2 4-pool-1-thread-1 5-pool-1-thread-4 6-pool-1-thread-3
class Tp{ public void Tp1() { final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,5, 1000,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<>(5), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) { threadPoolExecutor.execute( new TpRun(i) ); } } } class TpRun implements Runnable{ int index; public TpRun(int index){ this.index = index; } @Override public void run() { try { System.out.println(index+"-"+Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ################### 1-pool-1-thread-2 2-pool-1-thread-3 0-pool-1-thread-1 8-pool-1-thread-4 9-pool-1-thread-5 3-pool-1-thread-2 4-pool-1-thread-1 5-pool-1-thread-3 7-pool-1-thread-4 6-pool-1-thread-5
3-直接提交队列-AbortPolicy
class Tp{ public void Tp1() { final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,5, 1000,TimeUnit.MILLISECONDS,new SynchronousQueue<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) { threadPoolExecutor.execute( new TpRun(i) ); } } } class TpRun implements Runnable{ int index; public TpRun(int index){ this.index = index; } @Override public void run() { try { System.out.println(index+"-"+Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ####################### 0-pool-1-thread-1 1-pool-1-thread-2 2-pool-1-thread-3 3-pool-1-thread-4 4-pool-1-thread-5 Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task TpRun@135fbaa4 rejected from java.util.concurrent.ThreadPoolExecutor@45ee12a7[Running, pool size = 5, active threads = 5, queued tasks = 0, completed tasks = 0] at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063) at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830) at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379) at Tp.Tp1(MThread.java:21) at MThread.main(MThread.java:8)
4-无界的任务队列-AbortPolicy
class Tp{ public void Tp1() { final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,5, 1000,TimeUnit.MILLISECONDS,new LinkedBlockingDeque<>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) { threadPoolExecutor.execute( new TpRun(i) ); } } } class TpRun implements Runnable{ int index; public TpRun(int index){ this.index = index; } @Override public void run() { try { System.out.println(index+"-"+Thread.currentThread().getName()); Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ########################## 1-pool-1-thread-2 0-pool-1-thread-1 2-pool-1-thread-3 3-pool-1-thread-2 4-pool-1-thread-3 5-pool-1-thread-1 6-pool-1-thread-3 8-pool-1-thread-2 7-pool-1-thread-1 9-pool-1-thread-3

浙公网安备 33010602011771号