线程池(4)-参数-RejectedExecutionHandler
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的更多相关文章
- 线程池ThreadPoolExecutor参数设置
线程池ThreadPoolExecutor参数设置 JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同 ...
- Java线程池构造参数详解
在ThreadPoolExecutor类中有4个构造函数,最终调用的是如下函数: public ThreadPoolExecutor(int corePoolSize, int maximumPool ...
- ThreadPoolExecutor(线程池)的参数
构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit u ...
- 线程池ThreadPoolExecutor参数分析
概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...
- 线程池(3)-参数-实现ThreadFactory
1.介绍 ThreadFactory用来创建线程,需要实现newThread方法. 2.常用场景 线程重命名 设置守护进程 设置优先级 3.示例(线程重命名) public class ThreadF ...
- ThreadPoolExecutor线程池参数设置技巧
一.ThreadPoolExecutor的重要参数 corePoolSize:核心线程数 核心线程会一直存活,及时没有任务需要执行 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线 ...
- Java线程池使用和常用参数
多线程问题: 1.java中为什么要使用多线程使用多线程,可以把一些大任务分解成多个小任务来执行,多个小任务之间互不影像,同时进行,这样,充分利用了cpu资源. 2.java中简单的实现多线程的方式 ...
- Java线程池参数
关于Java线程池的参数设置.线程池是Java多线程里开发里的重要内容,使用难度不大,但如何用好就要明白参数的含义和如何去设置.干货里的内容大多是参考别人的,加入了一些知识点的扩充和看法.希望能对多线 ...
- java中的线程(3):线程池类 ThreadPoolExecutor「线程池的类型、参数、扩展等」
官方文档: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html 1.简介 pu ...
随机推荐
- 【转载】C#中List集合SingleOrDefault和FirstOrDefault方法有何不同
在C#的List集合类的操作过程中,有时候我们会使用到List集合的SingleOrDefault方法和FirstOrDefault等方法,这2个方法都是System.Linq.Enumerable类 ...
- TypeScript_基础数据类型
TypeScript 的基础数据类型包含: string.number.boolean.array .object.null.undefined.enmu.void.never.any.tuple 注 ...
- 安装node.js->npm->vue
我们研究vue时,首先操作的就是vue的引用,大部分人为了方便直接在页面上引用vue.js,但是一些大型网站还是比较喜欢用vue的npm命令来安装vue并使用,之前研究vue时,研究过使用npm安装的 ...
- Linux下用的脚本
http://blog.itpub.net/29510932/viewspace-1166603/ 批量启动Tomcat 点击(此处)折叠或打开 #!/bin/bash #JDK路径 export J ...
- 听说你知道什么是锁 --JAVA
I.java中的锁 1.1 什么是锁 在计算机科学中,锁(lock)与互斥(mutex)是一种同步机制,用于在许多线程执行时对资源的限制. 锁通常需要硬件支持才可以有效实施.这种支持通常采用一 ...
- http状态码记录
一些常见的状态码为: 200 - 服务器成功返回网页404 - 请求的网页不存在503 - 服务不可用详细分解: 1xx(临时响应)表示临时响应并需要请求者继续执行操作的状态代码. 代码 说明100 ...
- 【转】DSP动态内存分配函数的使用
DSP里的动态内存分配,其分配的内存区域在在堆(heap)中.同时DSP里动态分配内存的函数还有calloc以及reclloc.这些动态分配的内存放置在.system段的全局池或堆(heap)中.因此 ...
- php与ajax技术
web2.0的到来,ajax逐渐成为主流,什么是ajax,ajax的开发模式,优点,使用技术.(ajax概述,ajax使用的技术,需要注意的 问题,在PHP应用ajax技术的应用) 什么是ajax,a ...
- 腾讯云服务器搭建WampServer环境
软件环境Windows Server 2008 R2 企业版 SP1 64位 刚刚进入 Windows Server ,你会看到以下界面: 列出了服务器的基础信息和常用配置下载 XAMPP https ...
- Yarn Nodemanager启动不了报YarnRuntimeException: Failed to initialize container executor error=13 权限不够
1.现象:有一个节点的NodeManager启动不了. 后台报错日志如下: org.apache.hadoop.yarn.exceptions.YarnRuntimeException: Failed ...