1.介绍

当线程池线程数大于最大线程数(maximumPoolSize)时,多余的任务,程序应该按照什么拒绝策略处理。

2.拒绝策略4个

AbortPolicy:丢弃任务,并抛出RejectedExecutionException异常(需要在调用线程处捕获异常,即执行submit线程处)

DiscardPolicy:丢弃任务,不抛出异常

DiscardOldestPolicy:丢弃等待队列最前面任务

CallerRunsPolicy:由调用线程处理该任务

3.示例

3.1.AbortPolicy捕获异常

public class RejectedAbortPolicy {

    static class MyRunnable implements Runnable {
private String jobName; MyRunnable(String jobName) {
this.jobName = jobName;
} @Override
public void run() {
try {
while (true) {
Thread.currentThread();
Thread.sleep();
System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String[] args) throws InterruptedException {
ExecutorService es = new ThreadPoolExecutor(, , , TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(),
Executors.defaultThreadFactory(), new AbortPolicy());
es.submit(new MyRunnable("job-1"));
es.submit(new MyRunnable("job-2"));
es.submit(new MyRunnable("job-3"));
Thread.sleep();
try {
Future<?> f = es.submit(new MyRunnable("job-4"));
f.get();// 这一句永远执行不到
} catch (Exception e) {
System.err.println("job-4 submit Thread pool mistake");
}
}
}

3.2.DiscardOldestPolicy测试丢弃等待队列最前面任务

public class RejectedDiscardOldestPolicy {
static class MyRunnable implements Runnable {
private String jobName; MyRunnable(String jobName) {
this.jobName = jobName;
} public String getJobName(){
return jobName;
} @Override
public void run() {
try {
while (true) {
Thread.currentThread();
Thread.sleep();
System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>();
ExecutorService es = new ThreadPoolExecutor(, , , TimeUnit.SECONDS, workQueue,
Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
es.execute(new MyRunnable("job-1"));
es.execute(new MyRunnable("job-2"));
es.execute(new MyRunnable("job-3"));
Runnable firstRunnable = workQueue.poll();
System.err.println("submit job-3 检查队列当前 Runnable名称:" +((MyRunnable) firstRunnable).getJobName());
workQueue.add(firstRunnable); Thread.sleep();
es.execute(new MyRunnable("job-4"));
Runnable SecondRunnable = workQueue.poll();
System.err.println("submit job-4 检查队列当前 Runnable名称应该为job-4,实际为:" +((MyRunnable) SecondRunnable).getJobName());
workQueue.add(SecondRunnable);
}
}

3.3.CallerRunsPolicy主线程处理这个任务

public class RejectedCallerRunsPolicy {
static class MyRunnable implements Runnable {
private String jobName; MyRunnable(String jobName) {
this.jobName = jobName;
} public String getJobName(){
return jobName;
} @Override
public void run() {
try {
while (true) {
Thread.currentThread();
Thread.sleep();
System.err.println("当前线程:" + Thread.currentThread().getName() + " 当前任务:" + jobName);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>();
ExecutorService es = new ThreadPoolExecutor(, , , TimeUnit.SECONDS, workQueue,
Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
es.execute(new MyRunnable("job-1"));
es.execute(new MyRunnable("job-2"));
es.execute(new MyRunnable("job-3")); Thread.sleep();
es.execute(new MyRunnable("job-4"));
}
}

线程池(4)-参数-RejectedExecutionHandler的更多相关文章

  1. 线程池ThreadPoolExecutor参数设置

    线程池ThreadPoolExecutor参数设置 JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同 ...

  2. Java线程池构造参数详解

    在ThreadPoolExecutor类中有4个构造函数,最终调用的是如下函数: public ThreadPoolExecutor(int corePoolSize, int maximumPool ...

  3. ThreadPoolExecutor(线程池)的参数

    构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit u ...

  4. 线程池ThreadPoolExecutor参数分析

    概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...

  5. 线程池(3)-参数-实现ThreadFactory

    1.介绍 ThreadFactory用来创建线程,需要实现newThread方法. 2.常用场景 线程重命名 设置守护进程 设置优先级 3.示例(线程重命名) public class ThreadF ...

  6. ThreadPoolExecutor线程池参数设置技巧

    一.ThreadPoolExecutor的重要参数   corePoolSize:核心线程数 核心线程会一直存活,及时没有任务需要执行 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线 ...

  7. Java线程池使用和常用参数

    多线程问题: 1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源. 2.java中简单的实现多线程的方式 ...

  8. Java线程池参数

    关于Java线程池的参数设置.线程池是Java多线程里开发里的重要内容,使用难度不大,但如何用好就要明白参数的含义和如何去设置.干货里的内容大多是参考别人的,加入了一些知识点的扩充和看法.希望能对多线 ...

  9. java中的线程(3):线程池类 ThreadPoolExecutor「线程池的类型、参数、扩展等」

    官方文档: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html 1.简介 pu ...

随机推荐

  1. 爬虫之PyQuery的base了解

    爬虫之PyQuery的base了解 pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好,和它差不多的还有BeautifulSo ...

  2. 9.如何让一个div 上下左右居中?【CS

      方法1:[绝对定位50%-本身50%]              position:absolute; left:50%; top:50%;              transform: tra ...

  3. LINQ按多列分组(Group By)并计算总和(Sum) (转载)

    来源:https://codedefault.com/2018/group-by-multiple-columns-and-sum-in-csharp .NET[C#]LINQ按多列分组(Group ...

  4. Python学习日记(二) list操作

    l = ['a','b','c','d',1,2,[3,'e',4]] 1.list.append() 在list的结尾新增一个新的元素,没有返回值,但会修改原列表 l.append(5) print ...

  5. MySQL DataType--日期格式

    在MySQL中,无论是字符串转换为时间还是时间转换为字符串,都需要使用到时间格式: %a 缩写星期名 %b 缩写月名 %c 月,数值 %D 带有英文前缀的月中的天 %d 月的天,数值(00-31) % ...

  6. 一:MySQL系列之基本介绍(一)

    本篇主要介绍关于MySQL数据的基本知识,包括数据存储的变化,什么是MySQL以及其有什么优点.以及什么是RDBMS概念性知识等,以及关于MySQL语句的SOL的基本用法: 一.数据库 数据库,顾名思 ...

  7. linux 进程管理与调度(一)

    进程结构 进程在内核的源代码中以结构体表示,篇幅很长,在此列举一小段关键代码,可以发现是个双向链表,具体的可以在内核目录下找一个叫"sched.h"的头文件. struct tas ...

  8. javascript详解1

    推荐学习链接: https://book.apeland.cn/details/356/ http://es6.ruanyifeng.com/#README https://developer.moz ...

  9. Nginx网站用户认证

    一.Nginx网站用户认证 用户认证:用户访问网页时需要输入一个用户名和密码才能打开网页. nginx的默认网页时安装目录下的html/index.html,配置文件在安装目录下的conf目录中的ng ...

  10. Office2016专业增强版永久激活

    Office2016专业增强版永久激活码:Microsoft Office 2016 Pro Plus Retail Mak序列号XNTT9-CWMM3-RM2YM-D7KB2-JB6DVBHXN7- ...