线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用
作者QQ:1095737364 QQ群:123300273 欢迎加入!
1.线程池介绍
public ThreadPoolExecutor(
int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
RejectedExecutionHandler handler
);
2.运行策略
3.测试示例
(1)LinkedBlockingQueue<Runnable>队列使用1:
package threadQueueTest;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* User: 杨永生
* Date: 15:47 2017/8/8
* Email: kevin@hiibook.com
*/
public class ThreadPoolTest implements Runnable {
public void run() {
synchronized(this) {
try{
System.out.println(Thread.currentThread().getName());
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
}
} public static void main(String[] args) {
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 10; i++) {
executor.execute(new Thread(new ThreadPoolTest(),"TestThread".concat(""+i)));
int threadSize = queue.size();
System.out.println("线程队列大小为-->"+threadSize);
}
executor.shutdown();
}
}
(2)LinkedBlockingQueue<Runnable>队列使用2:
package threadQueueTest;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* User: 杨永生
* Date: 15:55 2017/8/8
* Email: kevin@hiibook.com
*/
public class ThreadPoolTest2 implements Runnable {
public void run() {
synchronized(this) {
try{
System.out.println("线程名称:"+Thread.currentThread().getName());
Thread.sleep(3000); //休眠是为了让该线程不至于执行完毕后从线程池里释放
}catch (InterruptedException e){
e.printStackTrace();
}
}
} public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(4); //固定为4的线程队列
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 10; i++) {
executor.execute(new Thread(new ThreadPoolTest2(), "TestThread".concat(""+i)));
int threadSize = queue.size();
System.out.println("线程队列大小为-->"+threadSize);
}
executor.shutdown();
}
}
(3)LinkedBlockingQueue<Runnable>队列使用3(测试异常):
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(4); //固定为4的线程队列
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 11; i++) {
executor.execute(new Thread(new ThreadPoolTest2(), "TestThread".concat(""+i)));
int threadSize = queue.size();
System.out.println("线程队列大小为-->"+threadSize);
}
executor.shutdown();
}
(4)LinkedBlockingQueue<Runnable>队列使用3(测试add(E e)异常):
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(4); //固定为4的线程队列
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 10; i++) {
executor.execute(new Thread(new ThreadPoolTest4(), "TestThread".concat(""+i)));
int threadSize = queue.size();
System.out.println("线程队列大小为-->"+threadSize);
if (threadSize==4){
queue.add(new Runnable() { //队列已满,抛异常
@Override
public void run(){
System.out.println("我是新线程,看看能不能搭个车加进去!");
}
});
}
}
executor.shutdown();
}
(5)LinkedBlockingQueue<Runnable>队列使用3(测试 offer(E e)异常):
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(4); //固定为4的线程队列
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 10; i++) {
executor.execute(new Thread(new ThreadPoolTest5(), "TestThread".concat(""+i)));
int threadSize = queue.size();
System.out.println("线程队列大小为-->"+threadSize);
if (threadSize==4){
final boolean flag = queue.offer(new Runnable() {
@Override
public void run(){
System.out.println("我是新线程,看看能不能搭个车加进去!");
}
});
System.out.println("添加新线程标志为-->"+flag);
}
}
executor.shutdown();
}
(6)LinkedBlockingQueue<Runnable>队列使用3(测试put(E e)异常):
public static void main(String[] args) throws InterruptedException {
BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(4); //固定为4的线程队列
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 6, 1, TimeUnit.DAYS, queue);
for (int i = 0; i < 10; i++) {
executor.execute(new Thread(new ThreadPoolTest6(), "TestThread".concat(""+i)));
int threadSize = queue.size();
System.out.println("线程队列大小为-->"+threadSize);
if (threadSize==4){
queue.put(new Runnable() {
@Override
public void run(){
System.out.println("我是新线程,看看能不能搭个车加进去!");
}
});
}
}
executor.shutdown();
}
4.总结:

版权声明: 本文有 ```...裥簞點 发表于 bloghome博客
转载声明: 可自由转载、引用,但需要属名作者且注明文章出处。
线程池ThreadPoolExecutor与阻塞队列BlockingQueue应用的更多相关文章
- 转:JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue
从Java5开始,Java提供了自己的线程池.每次只执行指定数量的线程,java.util.concurrent.ThreadPoolExecutor 就是这样的线程池.以下是我的学习过程. 首先是构 ...
- spring线程池ThreadPoolTaskExecutor与阻塞队列BlockingQueue
一: ThreadPoolTaskExecutor是一个spring的线程池技术,查看代码可以看到这样一个字段: private ThreadPoolExecutor threadPoolExecut ...
- java多线程:线程池原理、阻塞队列
一.线程池定义和使用 jdk 1.5 之后就引入了线程池. 1.1 定义 从上面的空间切换看得出来,线程是稀缺资源,它的创建与销毁是一个相对偏重且耗资源的操作,而Java线程依赖于内核线程,创建线程需 ...
- java线程(7)——阻塞队列BlockingQueue
回顾: 阻塞队列,英文名叫BlockingQueue.首先他是一种队列,联系之前Java基础--集合中介绍的Queue与Collection,我们就很容易开始今天的阻塞队列的学习了.来看一下他们的接口 ...
- 多线程学习笔记八之线程池ThreadPoolExecutor实现分析
目录 简介 继承结构 实现分析 ThreadPoolExecutor类属性 线程池状态 构造方法 execute(Runnable command) addWorker(Runnable firstT ...
- 21.线程池ThreadPoolExecutor实现原理
1. 为什么要使用线程池 在实际使用中,线程是很占用系统资源的,如果对线程管理不善很容易导致系统问题.因此,在大多数并发框架中都会使用线程池来管理线程,使用线程池管理线程主要有如下好处: 降低资源消耗 ...
- 常用阻塞队列 BlockingQueue 有哪些?
为什么要使用阻塞队列 之前,介绍了一下 ThreadPoolExecutor 的各参数的含义(并发编程之线程池ThreadPoolExecutor),其中有一个 BlockingQueue,它是一个阻 ...
- [转] 引用 Java自带的线程池ThreadPoolExecutor详细介绍说明和实例应用
PS: Spring ThreadPoolTaskExecutor vs Java Executorservice cachedthreadpool 引用 [轰隆隆] 的 Java自带的线程池Thre ...
- java线程API学习 线程池ThreadPoolExecutor(转)
线程池ThreadPoolExecutor继承自ExecutorService.是jdk1.5加入的新特性,将提交执行的任务在内部线程池中的可用线程中执行. 构造函数 ThreadPoolExecut ...
随机推荐
- Java总结之线程(1)
java线程是很重要的一项,所以作为java程序员必须要掌握的. 理解java线程必须先理解线程在java中的生命周期.. 1.java线程生命周期 1.new 创建一个线程 java中创建线程有 ...
- Python系列教程(三):输入和输出
1.1 raw_input() 在Python中,获取键盘输入的数据的方法是采用 raw_input 函数(至于什么是函数,咱们以后的章节中讲解),那么这个 raw_input 怎么用呢? 看如下示例 ...
- 懒人的小技巧, 批处理修改IP
相信很多人都有这样的麻烦, 工作单位的IP网段与住的不一致, 自己的笔记本在单位和回家的时候每次都要更改IP, 很麻烦, 偷个懒, 做了个批处理来修改IP,方便一点. 还有就是可以把工作的时候才需要 ...
- winform利用委托delegate进行窗体间通信
前段时间学习委托,感觉很模糊的样子,也做过许多实例,但是项目中一直没有用到,今天在项目中遇到一个很简单的例子,现在拿出来,做一个简单的记录. 要求:将弹出框里勾选的内容返回到主面板上. 工具:委托. ...
- (转载)MQ基本操作
摘自:http://blog.sina.com.cn/s/blog_4892cf780100erga.html 一.MQ基本操作 MQ中有几个很重要的组件:队列管理器(QueueManager).队列 ...
- maven Spring+Spring MVC+Mybatis+mysql轻量级Java web开发环境搭建
之前一直在做的一个GIS系统项目,采用了jsp+servlet框架,数据传输框架采用了apache的thrift框架,短时多传的风格还不错,但是较其他的java web项目显得有点太臃肿了,现在给大家 ...
- # nodejs模块学习: express 解析
# nodejs模块学习: express 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子 ...
- PHP发送E-mail---新手教程
首先下载PHPmailer拓展包,其实就是别人封装好的类库,下载链接:http://pan.baidu.com/s/1slbhGo1 首先去163注册个账号,然后登陆进去,点击设置下面的 POP3/S ...
- HTTP消息中Header头部信息整理
1.HTTP请求方式 GET 向Web服务器请求一个文件 POST 向Web服务器发送数据让Web服务器进行处理 PUT 向Web服务器发送数据并存储在Web服务器内部 HEAD 检查一个对象是否存在 ...
- Java创建连接池连接不同数据库
在一个应用里面,可能涉及到连接多个不同数据库进行操作,而每次连接写不同的实现会很麻烦.前面已经会了用JDBC连接数据库,那么利用反射和工厂模式,可以实现连接不同的数据库,这样处理起来将会很方便.同时建 ...