线程池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 ...
随机推荐
- [leetcode-604-Design Compressed String Iterator]
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- 【Android Developers Training】 32. 向其它应用发送简单数据
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Example007关闭窗口时关闭父窗口
<!--实例007关闭窗口时刷新父窗口--> <!DOCTYPE html> <html lang="en"> <head> < ...
- 14.什么是jsp动作
JSP动作元素(action elements),动作元素为请求处理阶段提供信息.动作元素遵循XML元素的语法,有一个包含元素名的开始标签,可以有属性,可选的内容,与开始标签匹配的结束标签. 包含的类 ...
- centos中NAT模式下静态IP连接外网
使用linux虚拟机时,通常会用到yum命令来安装软件,然而这个命令需要连外网下载软件,用maven下载jar包也需要外网.虚拟机在内网可以互相ping通,然而ping不了外网,于是通过试验,终于找到 ...
- 编写高质量代码改善程序的157个建议:第87个建议之区分WPF和WinForm的线程模型
今天有时间了,继续<编写高质量代码改善程序的157个建议>的阅读,当我阅读到建议87的时候,里面的一些代码示例和文中所说的不一致了,是不是我现在用的是NetFramework 4.0的缘故 ...
- js加强版图片轮播
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js实现两个输入框中的数字相乘并自动将结果显示在第三个输入框
<script type="text/javascript"> function cal(ida,idb,idc) { var numa=Number(document ...
- VB6之断点续传
闲来无事,研究了下HTTP的断点续传,用VB6写了小Demo. 关于HTTP-Range细节可参考: http://www.w3.org/Protocols/rfc2616/rfc2616.html ...
- sublime使用总结
上周忙呀忙~ 周一到五在忙项目,周六日搬家 在帝都平均一年就要换一次房子,从开始找房子到成功住进去前前后后大约花了半个多月的时间 什么时候就有自己的小窝了-- 之前开发一直用的都是W ...