基于线程池的线程管理(BlockingQueue生产者消费者方式)实例
1.线程池管理类:
public class ThreadPoolManager {
private static ThreadPoolManager instance = new ThreadPoolManager();
private ExecutorService secLogThreadPool;
private ExecutorService sysLogThreadPool;
public ExecutorService getSysLogThreadPool() {
return sysLogThreadPool;
}
public void setSysLogThreadPool(ExecutorService sysLogThreadPool) {
this.sysLogThreadPool = sysLogThreadPool;
}
public ExecutorService getSecLogThreadPool() {
return secLogThreadPool;
}
public void setSecLogThreadPool(ExecutorService secLogThreadPool) {
this.secLogThreadPool = secLogThreadPool;
}
public static ThreadPoolManager getInstance(){
return instance;
}
private ThreadPoolManager() {
secLogThreadPool = new ThreadPoolExecutor(1, 3, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2000), new ThreadPoolExecutor.CallerRunsPolicy());
sysLogThreadPool = Executors.newFixedThreadPool(3);
}
}
注:
线程池类为 Java.util.concurrent.ThreadPoolExecutor,常用构造方法为:
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler)
参数含义如下:
corePoolSize: 线程池维护线程的最少数量
maximumPoolSize:线程池维护线程的最大数量
keepAliveTime: 线程池维护线程所允许的空闲时间
unit: 线程池维护线程所允许的空闲时间的单位
workQueue: 线程池所使用的缓冲队列
handler: 线程池对拒绝任务的处理策略
2. 生产者类:
public class SecLogProduceThread implements Runnable {
SecLogEntity entity = null;
public SecLogProduceThread(SecLogEntity entity) {
this.entity = entity;
}
@Override
public void run() {
SecLogStorage.getInstance().produce(entity);
}
}
3.消费者类:
public class SecLogConsumeThread implements Runnable {
@Override
public void run() {
while(true){
//TODO do something here
SecLogStorage.getInstance().consume();
}
}
}
4.日志仓储类:BlockingQueue方式
public class SecLogStorage {
private final int MAX_SIZE = 100;
private LinkedBlockingDeque<SecLogEntity> list = new LinkedBlockingDeque<SecLogEntity>(MAX_SIZE);
private static SecLogStorage instance = new SecLogStorage();
private SecLogStorage() {
}
public static SecLogStorage getInstance() {
return instance;
}
public void produce(SecLogEntity seclog) {
if (list.size() == MAX_SIZE) {
System.out.println("seclog库存量为" + MAX_SIZE + ",不能再继续生产!");
}
try {
list.put(seclog);
System.out.println("生产SecLog:"+ JSONObject.fromObject(seclog));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public SecLogEntity consume(){
SecLogEntity entity = null;
if(list.isEmpty()){
System.out.println("seclog库存量为0,不能再继续消费!");
}
try {
entity = list.take();
System.out.println("消费SecLog:"+JSONObject.fromObject(entity));
} catch (InterruptedException e) {
e.printStackTrace();
}
return entity;
}
}
5. log bean :
public class SecLogEntity {
private String logName;
private String logSrc;
public String getLogName() {
return logName;
}
public void setLogName(String logName) {
this.logName = logName;
}
public String getLogSrc() {
return logSrc;
}
public void setLogSrc(String logSrc) {
this.logSrc = logSrc;
}
}
6. 测试类:
public class ThreadPoolTest {
public static void main(String[] args) {
SecLogEntity log1 = new SecLogEntity();
log1.setLogName("test1");
log1.setLogSrc("seclog1");
SecLogEntity log2 = new SecLogEntity();
log2.setLogName("test2");
log2.setLogSrc("seclog2");
SysLogEntity log3 = new SysLogEntity();
log3.setLogName("test3");
log3.setLogSrc("syslog1");
SysLogEntity log4 = new SysLogEntity();
log4.setLogName("test4");
log4.setLogSrc("syslog2");
ThreadPoolManager.getInstance().getSecLogThreadPool().execute(new SecLogProduceThread(log1));
ThreadPoolManager.getInstance().getSecLogThreadPool().execute(new SecLogProduceThread(log2));
ThreadPoolManager.getInstance().getSecLogThreadPool().execute(new SecLogConsumeThread());
ThreadPoolManager.getInstance().getSysLogThreadPool().execute(new SysLogProduceThread(log3));
ThreadPoolManager.getInstance().getSysLogThreadPool().execute(new SysLogProduceThread(log4));
ThreadPoolManager.getInstance().getSysLogThreadPool().execute(new SysLogConsumeThread());
}
}
7. 测试结果:
生产SecLog:{"logName":"test1","logSrc":"seclog1"}
生产syslog:{"logName":"test3","logSrc":"syslog1"}
消费syslog: {"logName":"test3","logSrc":"syslog1"}
生产SecLog:{"logName":"test2","logSrc":"seclog2"}
消费syslog: {"logName":"test4","logSrc":"syslog2"}
syslog库存量为0,无法再消费syslog!
生产syslog:{"logName":"test4","logSrc":"syslog2"}
消费SecLog:{"logName":"test1","logSrc":"seclog1"}
消费SecLog:{"logName":"test2","logSrc":"seclog2"}
seclog库存量为0,不能再继续消费!
基于线程池的线程管理(BlockingQueue生产者消费者方式)实例的更多相关文章
- Java并发编程:4种线程池和缓冲队列BlockingQueue
一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池.使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动 ...
- 由浅入深理解Java线程池及线程池的如何使用
前言 多线程的异步执行方式,虽然能够最大限度发挥多核计算机的计算能力,但是如果不加控制,反而会对系统造成负担.线程本身也要占用内存空间,大量的线程会占用内存资源并且可能会导致Out of Memory ...
- 高并发的epoll+线程池,线程池专注实现业务
我们知道,服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下 ...
- Java多线程系列 JUC线程池02 线程池原理解析(一)
转载 http://www.cnblogs.com/skywang12345/p/3509960.html ; http://www.cnblogs.com/skywang12345/p/35099 ...
- java并发编程(十五)----(线程池)java线程池简介
好的软件设计不建议手动创建和销毁线程.线程的创建和销毁是非常耗 CPU 和内存的,因为这需要 JVM 和操作系统的参与.64位 JVM 默认线程栈是大小1 MB.这就是为什么说在请求频繁时为每个小的请 ...
- ReentrantLock+线程池+同步+线程锁
1.并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量 ...
- Java多线程系列 JUC线程池01 线程池框架
转载 http://www.cnblogs.com/skywang12345/p/3509903.html 为什么引入Executor线程池框架 new Thread()的缺点 1. 每次new T ...
- java并发编程(十七)----(线程池)java线程池架构和原理
前面我们简单介绍了线程池的使用,但是对于其如何运行我们还不清楚,Executors为我们提供了简单的线程工厂类,但是我们知道ThreadPoolExecutor是线程池的具体实现类.我们先从他开始分析 ...
- java 线程池(线程的复用)
一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池.使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动 ...
随机推荐
- Django_ajax
AJAX(Asynchronous Javascript And XML)翻译成中文就是"异步Javascript和XML".即使用Javascript语言与服务器进行异步交互,传 ...
- Codeforces 839B Game of the Rows【贪心】
B. Game of the Rows time limit per test:1 second memory limit per test:256 megabytes input:standard ...
- Vijos P1784 数字统计【模拟】
数字统计 背景 来自 NOIP2010 普及组 第一题 描述 请统计某个给定范围[L, R]的所有整数中,数字2出现的次数. 比如在给定范围[2, 22],数字2在数2中出现了1次,在数12中出现了1 ...
- Exponentiation(java 大实数)
http://acm.hdu.edu.cn/showproblem.php?pid=1063 Exponentiation Time Limit: 2000/500 MS (Java/Others) ...
- Socket send函数和recv函数详解
1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP ...
- oracle创建触发器及作用举例
--创建触发器及作用举例 create or replace trigger tri before delete on emp --在删除emp表数据之前需要做的事根据自己的业务去写,before是在 ...
- Node.js系列-http
前言: 最近一直忙着公司项目的事,战友们的留言也没空回复,博客也有段时间没有更新了,年底了就是一个的忙啊~~~(ps:同感的也给个赞吧) 现在前端的就是一直地更新一直有新的东西出来,什么ES2015, ...
- 把织梦安装到子目录,不读取CSS 没有样式?
我在A5上找的一个模板,照着说明安装到根目录就正常,我想安装到子目录下面,结果很乱 应该是不读取CSS. {dede:global.cfg_templets_skin/}/style/about.cs ...
- SMTP错误码建议解决方法
https://wenku.baidu.com/view/0af30e01e87101f69e3195b8.html SMTP 错误码 / 建议解决方法 错误总表 101 Cannot Open Co ...
- php对数组进行分页
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...