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生产者消费者方式)实例的更多相关文章

  1. Java并发编程:4种线程池和缓冲队列BlockingQueue

    一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池.使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动 ...

  2. 由浅入深理解Java线程池及线程池的如何使用

    前言 多线程的异步执行方式,虽然能够最大限度发挥多核计算机的计算能力,但是如果不加控制,反而会对系统造成负担.线程本身也要占用内存空间,大量的线程会占用内存资源并且可能会导致Out of Memory ...

  3. 高并发的epoll+线程池,线程池专注实现业务

    我们知道,服务器并发模型通常可分为单线程和多线程模型,这里的线程通常是指“I/O线程”,即负责I/O操作,协调分配任务的“管理线程”,而实际的请求和任务通常交由所谓“工作者线程”处理.通常多线程模型下 ...

  4. Java多线程系列 JUC线程池02 线程池原理解析(一)

    转载  http://www.cnblogs.com/skywang12345/p/3509960.html ; http://www.cnblogs.com/skywang12345/p/35099 ...

  5. java并发编程(十五)----(线程池)java线程池简介

    好的软件设计不建议手动创建和销毁线程.线程的创建和销毁是非常耗 CPU 和内存的,因为这需要 JVM 和操作系统的参与.64位 JVM 默认线程栈是大小1 MB.这就是为什么说在请求频繁时为每个小的请 ...

  6. ReentrantLock+线程池+同步+线程锁

    1.并发编程三要素? 1)原子性 原子性指的是一个或者多个操作,要么全部执行并且在执行的过程中不被其他操作打断,要么就全部都不执行. 2)可见性 可见性指多个线程操作一个共享变量时,其中一个线程对变量 ...

  7. Java多线程系列 JUC线程池01 线程池框架

    转载  http://www.cnblogs.com/skywang12345/p/3509903.html 为什么引入Executor线程池框架 new Thread()的缺点 1. 每次new T ...

  8. java并发编程(十七)----(线程池)java线程池架构和原理

    前面我们简单介绍了线程池的使用,但是对于其如何运行我们还不清楚,Executors为我们提供了简单的线程工厂类,但是我们知道ThreadPoolExecutor是线程池的具体实现类.我们先从他开始分析 ...

  9. java 线程池(线程的复用)

    一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池.使用线程池可以很好地提高性能,线程池在系统启动时即创建大量空闲的线程,程序将一个任务传给线程池,线程池就会启动 ...

随机推荐

  1. 枚举 输入流重载>> C++

    语言:C++ 我们可能会遇到这种情况:自己定义了一个枚举类型,但是却不知道如何重载输入流,使我们定义的枚举用起来不是很方便. 那么,如何去重载呢,我们先来看一下内置类型的测试过程: #include& ...

  2. BZOJ 3670: [Noi2014]动物园【KMP变形 】

    3670: [Noi2014]动物园 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2738  Solved: 1475[Submit][Status ...

  3. c++(爬楼梯)

    前两天上网的时候看到一个特别有意思的题目,在这里和朋友们分享一下: 有一个人准备开始爬楼梯,假设楼梯有n个,这个人只允许一次爬一个楼梯或者一次爬两个楼梯,请问有多少种爬法? 在揭晓答案之前,朋友们可以 ...

  4. win10清除桌面快捷方式小箭头

    reg add /d "%systemroot%\system32\imageres.dll,197" /t reg_sz /f taskkill /f /im explorer. ...

  5. JXLS 2.4.0系列教程(五)——更进一步的应用和页面边距bug修复

    注:本文代码建立于前面写的代码.不过不看也不要紧. 前面的文章把JXLS 2.4.0 的基本使用写了一遍,现在讲讲一些更进一步的使用方法.我只写一些我用到过的方法,更多的高级使用方法请参考官网. ht ...

  6. Debug模式下程序卡

    Debug通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release称为发布版本,它往往是进行了各种优化,使得程序在代码大小和运行速度上都是最优的,以便用户很好地使用. D ...

  7. [国嵌攻略][154][Linux-I2C子系统]

    IIC子系统架构 device driver层: 1.device driver,由用户开发. 2.i2c-dev由内核实现,但是需要配合应用模式驱动才能使用. i2c core层: 1.总线驱动,也 ...

  8. SQL Server 使用问题解答(持续更新中)

    问题一:sql server 2014不允许保存更改,您所做的更改要求删除并重新创建以下表 解答:工具-选项-不勾选组织保存要求重新创建表的更改,如下图确定.

  9. 用php怎样将图片gif转化为jpg

    <?php $input= "link2.gif"; $output='test.jpg' ; $image=imagecreatefromgif($input); imag ...

  10. dedecms利用memberlist标签调用自定义会员模型的会员信息

    [摘要]本文讲一下dedecms如何利用memberlist标签调用自定义会员模型的会员信息. dedecms利用memberlist标签调用自定义会员模型的会员信息,这个问题找了很久,官方论坛提问过 ...