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. angular 8 配置路由

    一.生成路由文件 按照惯例,有一个独立模块来配置相关路由,这个模块类的名字叫做AppRoutingModule,位于src/app下的app-routing.module.ts文件中. 使用CLI生成 ...

  2. iOS数组遍历

    对于一个数组 NSArray *array = @[@"111",@"222",@"333",@"444",@" ...

  3. 《Clean Code》读书笔记——第二周

    本周我阅读了<Clean Code>. “神在细节中!”,建筑家范德罗如是说.他当然专注于基于宏伟构架之上的永恒建筑形式,他也同样为自己设计的建筑挑选门把手.同样软件开发也是这样,小处见大 ...

  4. hash文件-对文件进行数字签名

    (一)windows自带hash命令: certutil -hashfile D:\1.exe MD5              #  md5的hash值为32位certutil -hashfile ...

  5. linux下补丁制作和使用方法

    两个文件的情况: 制作补丁: $ diff test1.c test2.c > test.patch 给test1.c打补丁: $ patch test1.c < test.patch 还 ...

  6. Kotlin枚举与委托深入详解

    枚举: 基本上跟Java的差不多,这里就过一遍既可,如下: 还可以接收参数,如下: 枚举还可以定义方法,如下: 看下错误提示: 所以可以这样: 然后咱们再冒号之前定义对象,如下: 下面来使用一下: 当 ...

  7. 分布式调度平台XXL-JOB源码分析-执行器端

    上一篇文章已经说到调度中心端如何进行任务管理及调度,本文将分析执行器端是如何接收到任务调度请求,然后执行业务代码的. XxlJobExecutorApplication为我们执行器的启动项,其中有个X ...

  8. go cache

    go 编译, 或是安装库的时候,产生的日志量很大 go env 删除掉这个log.txt文件,系统空间瞬间就够了

  9. spring-boot maven插件

    Spring Boot Maven Plugin提供了Spring Boot的Maven支持,允许你打包可执行文件和war文件,并且就地运行. 1.Spring Boot Maven plugin的5 ...

  10. python数据分析之数据分布

    转自链接:https://blog.csdn.net/YEPAO01/article/details/99197487 一.查看数据分布趋势 import pandas as pd import nu ...