20.custom自定义线程池
自定义线程池
1.若Executors工厂类无法满足需求,可以自己使用工厂类创建线程池,底层都是使用了ThreadPoolExecutor这个类可以自定义。
public ThreadPoolExecutor(int corePoolSize(当前核心线程数),
int maximunPoolSize(最大线程数),
long keepAliveTime(保持活跃时间),
TimeUnit utin(时间单位),
BlockingQueue<Runnable> workQueue(线程队列容器),
ThreadFactory threadFactory(线程工厂),
RejectedExecutionHandler rejectedExecutionHandler(拒绝执行的方法,线程队列阻塞到容器等待、例:限制最大执行、));
则会将任务加入队 列中,若队列已经满,则在总线程数不大于maximumPoolSize(最大coreRoolSize数)的前提下。创建一个新的线程,若大于 maximumPoolSize则实行拒绝策略、或其他处理方式。
implements RejectedExecutionHandle
package demo8.threadPool;
import java.util.concurrent.*;
public class CustomThreadPool1 {
/* 有界队列ArrayBlockingQueue:
若有新的线程需要执行,日光线程池实际数小于"coreRoolSize",则优先参加线程,若大于,则会将任务加入队列中,若队列已经满,则在总线程
数不大于maximumPoolSize(最大coreRoolSize数)的前提下。创建一个新的线程,若大于maximumPoolSize则实行拒绝策略、或其他处理方式。*/
public static void main(String[] args) {
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(3);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,
2,
60,
TimeUnit.SECONDS,
blockingQueue);
UserTask userTask1 = new UserTask(1, "任务-1");
UserTask userTask2 = new UserTask(2, "任务-2");
UserTask userTask3 = new UserTask(3, "任务-3");
UserTask userTask4 = new UserTask(4, "任务-4");
UserTask userTask5 = new UserTask(5, "任务-5");
threadPoolExecutor.execute(userTask1);
threadPoolExecutor.execute(userTask2);
threadPoolExecutor.execute(userTask3);
threadPoolExecutor.execute(userTask4);
threadPoolExecutor.execute(userTask5);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPoolExecutor.shutdown();
}
}
输出:
pool-1-thread-1 run taskId:1
这种情况处理完所有的任务
pool-1-thread-2 run taskId:5
pool-1-thread-2 run taskId:3
pool-1-thread-1 run taskId:2
pool-1-thread-2 run taskId:4
package demo8.threadPool;
/**
* Created by liudan on 2017/7/23.
*/
public class UserTask implements Runnable {
private int id;
private String taskName;
@Override
public void run() {
try {
Thread.sleep(1000);
System.err.println(Thread.currentThread().getName()+"\t run taskId:"+this.getId());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public UserTask() {
}
public UserTask(int id, String taskName) {
this.id = id;
this.taskName = taskName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
@Override
public String toString() {
return "UserTask{" +
"id=" + id +
'}';
}
}
案例2:拒绝任务,超出暂缓任务对了大小
public static void main(String[] args) {
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(3);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,
2,
60,
TimeUnit.SECONDS,
blockingQueue);
UserTask userTask1 = new UserTask(1, "任务-1");
UserTask userTask2 = new UserTask(2, "任务-2");
UserTask userTask3 = new UserTask(3, "任务-3");
UserTask userTask4 = new UserTask(4, "任务-4");
UserTask userTask5 = new UserTask(5, "任务-5");
UserTask userTask6 = new UserTask(6, "任务-6");多加一个
threadPoolExecutor.execute(userTask1);
threadPoolExecutor.execute(userTask2);
threadPoolExecutor.execute(userTask3);
threadPoolExecutor.execute(userTask4);
threadPoolExecutor.execute(userTask5);
threadPoolExecutor.execute(userTask6);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPoolExecutor.shutdown();
}
输出:
Exception in thread "main" java.util.concurrent.RejectedExecutionException:
Task UserTask{id=6} rejected from java.util.concurrent.ThreadPoolExecutor@6f94fa3e
[Running, pool size = 2, active threads = 2, queued tasks = 3, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
at demo8.threadPool.CustomThreadPool1.main(CustomThreadPool1.java:35)
pool-1-thread-1 run taskId:1
pool-1-thread-2 run taskId:5
pool-1-thread-1 run taskId:2
pool-1-thread-2 run taskId:3
pool-1-thread-1 run taskId:4
package demo8.threadPool;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
public class CustomDiscardPolicy implements RejectedExecutionHandler {
public CustomDiscardPolicy() {
}
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.err.println("拒绝任务");
/**
* 用日志记录保存下,可做后续的扩展操作
*/
System.err.println("加入任务log...\t"+r.toString());
}
}
public static void main(String[] args) {
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<Runnable>(3);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1,
2,
60,
TimeUnit.SECONDS,
blockingQueue,
new CustomDiscardPolicy());//使用自定义拒绝策略
UserTask userTask1 = new UserTask(1, "任务-1");
UserTask userTask2 = new UserTask(2, "任务-2");
UserTask userTask3 = new UserTask(3, "任务-3");
UserTask userTask4 = new UserTask(4, "任务-4");
UserTask userTask5 = new UserTask(5, "任务-5");
UserTask userTask6 = new UserTask(6, "任务-6");
threadPoolExecutor.execute(userTask1);
threadPoolExecutor.execute(userTask2);
threadPoolExecutor.execute(userTask3);
threadPoolExecutor.execute(userTask4);
threadPoolExecutor.execute(userTask5);
threadPoolExecutor.execute(userTask6);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
threadPoolExecutor.shutdown();
}
输出:
拒绝任务
加入任务log... UserTask{id=6}
pool-1-thread-1 run taskId:1
pool-1-thread-2 run taskId:5
pool-1-thread-1 run taskId:2
pool-1-thread-2 run taskId:3
pool-1-thread-1 run taskId:4
package demo8.threadPool;
import java.util.concurrent.*;
public class CustomThreadPool2 {
/* 无界队列LinkedBlockingQueue:
与有界队列相比,除非系统资源耗尽,否则无界的任务队列不存在加入任务队列失败的情况下,当有新的任务到来,系统的线程数小于coreRoolSize
时候,则新建线程执行任务,当到达coreRoolSize后就不会增加了,若后续还有新的任务加入,而且没有空闲的线程资源,则任务直接进入队列等待
。若任务创建和处理速度差异很大,无界队列保持快速增长,知道耗尽系统内存。*/
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,10,120L, TimeUnit.SECONDS,blockingQueue);
//ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(4);
for (int i=1;i<=20;i++){
threadPoolExecutor.execute(new UserTask(i,"任务-"+i));
}
Thread.sleep(1000);
System.err.println("blockingQueue size:"+blockingQueue.size());
Thread.sleep(2000);
}
}
输出:
pool-1-thread-1 run taskId:1
pool-1-thread-2 run taskId:2
pool-1-thread-3 run taskId:3
pool-1-thread-4 run taskId:4
pool-1-thread-5 run taskId:5
blockingQueue size:10
pool-1-thread-2 run taskId:7
pool-1-thread-4 run taskId:9
pool-1-thread-3 run taskId:8
pool-1-thread-1 run taskId:6
pool-1-thread-5 run taskId:10
pool-1-thread-2 run taskId:11
pool-1-thread-1 run taskId:14
pool-1-thread-3 run taskId:13
pool-1-thread-4 run taskId:12
pool-1-thread-5 run taskId:15
pool-1-thread-1 run taskId:17
pool-1-thread-4 run taskId:19
pool-1-thread-5 run taskId:20
pool-1-thread-2 run taskId:16
pool-1-thread-3 run taskId:18
20.custom自定义线程池的更多相关文章
- Android线程管理之ThreadPoolExecutor自定义线程池
前言: 上篇主要介绍了使用线程池的好处以及ExecutorService接口,然后学习了通过Executors工厂类生成满足不同需求的简单线程池,但是有时候我们需要相对复杂的线程池的时候就需要我们自己 ...
- 一个自定义线程池的小Demo
在项目中如果是web请求时候,IIS会自动分配一个线程来进行处理,如果很多个应用程序共享公用一个IIS的时候,线程分配可能会出现一个问题(当然也是我的需求造成的) 之前在做项目的时候,有一个需求,就是 ...
- C#自定义线程池
自定义线程池-c#的简单实现 下面是代码,希望大家提出更好的建议: 1.ThreadManager.cs using System; using System.Threading; using Sys ...
- SOFA 源码分析 — 自定义线程池原理
前言 在 SOFA-RPC 的官方介绍里,介绍了自定义线程池,可以为指定服务设置一个独立的业务线程池,和 SOFARPC 自身的业务线程池是隔离的.多个服务可以共用一个独立的线程池. API使用方式如 ...
- Spring Boot使用@Async实现异步调用:自定义线程池
前面的章节中,我们介绍了使用@Async注解来实现异步调用,但是,对于这些异步执行的控制是我们保障自身应用健康的基本技能.本文我们就来学习一下,如果通过自定义线程池的方式来控制异步调用的并发. 定义线 ...
- SpringBoot 自定义线程池
本教程目录: 自定义线程池 配置spring默认的线程池 1. 自定义线程池 1.1 修改application.properties task.pool.corePoolSize=20 task.p ...
- SpringBoot 自定义线程池,多线程
原文:https://www.jianshu.com/p/832f2b162450 我们都知道spring只是为我们简单的处理线程池,每次用到线程总会new 一个新的线程,效率不高,所以我们需要自定义 ...
- Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...
- Android 自定义线程池的实战
前言:在上一篇文章中我们讲到了AsyncTask的基本使用.AsyncTask的封装.AsyncTask 的串行/并行线程队列.自定义线程池.线程池的快速创建方式. 对线程池不了解的同学可以先看 An ...
随机推荐
- kubeadm高可用master节点部署文档
kubeadm的标准部署里,etcd和master都是单节点的. 但上生产,至少得高可用. etcd的高可用,用kubeadm微微扩散一下就可以. 但master却官方没有提及. 于是搜索了几篇文档, ...
- django rest_framework中将json输出字符强制为utf-8编码
最近在和日本外包合作开发JIRA对接发布系统的版本单时, 遇到这个问题. 就是我们这边的输出浏览器显示为中文,而到了JIRA端就出现乱码. 查了文档,原来django rest_framework的默 ...
- day2 字典常用的方法
字典创建的方式: (1)d1 = {"k1":"v1","k2":"v2","k3":&qu ...
- python开发学习-day04(迭代器、生成器、装饰器、二分查找、正则)
s12-20160123-day04 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- bzoj 1880 最短路径图
#include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...
- Django+Nginx+uwsgi搭建自己的博客(三)
(本来打算在这篇博文中介绍Users App的前端部分的,但写着写着就发现还需要铺垫很多东西才能把整个项目串的比较流畅些,因此这篇就继续介绍了后端的一些东西,前端的部分只好跳票到下一篇了-) 在上一篇 ...
- Contains,Exists,Any,Count 比较是否存在某个元素
private static void Main(string[] args) { ; Console.WriteLine("判断是否存在某个元素 :"); Console.Wri ...
- 获取token
获取token 提示:openstack 这个是获取N版的方法 ,主要区别在于这个路径上(http://192.168.0.228:35357/v3/auth/tokens ),以前版本可能会是v2 ...
- 【BZOJ 3043】 3043: IncDec Sequence (差分)
3043: IncDec Sequence Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 589 Solved: 332 Description 给 ...
- Opencv学习笔记2:图像模糊作用和方法
一.意义和作用: 图像的模糊处理就是将图片处理的更加模糊,如下图,左侧是原图,右侧是经过处理之后的图片. 从主观意愿上说,我们希望看到清晰的图像,而不是模糊的图像.所以很多时候我们听说还有一种专门进行 ...