Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
来源:互联网 发布:mac 使用oracle 编辑:程序博客网 时间:2024/06/06 07:39
http://heipark.iteye.com/blog/1393847
newFixedThreadPool内部有个任务队列,假设线程池里有3个线程,提交了5个任务,那么后两个任务就放在任务队列了,即使前3个任务sleep或者堵塞了,也不会执行后两个任务,除非前三个任务有执行完的
newFixedThreadPool使用范例:
- import java.io.IOException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class Test {
- public static void main(String[] args) throws IOException, InterruptedException {
- ExecutorService service = Executors.newFixedThreadPool(2);
- for (int i = 0; i < 6; i++) {
- final int index = i;
- System.out.println("task: " + (i+1));
- Runnable run = new Runnable() {
- @Override
- public void run() {
- System.out.println("thread start" + index);
- try {
- Thread.sleep(Long.MAX_VALUE);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("thread end" + index);
- }
- };
- service.execute(run);
- }
- }
- }
输出:
task: 1
task: 2
thread start0
task: 3
task: 4
task: 5
task: 6
task: 7
thread start1
task: 8
task: 9
task: 10
task: 11
task: 12
task: 13
task: 14
task: 15
task: 2
thread start0
task: 3
task: 4
task: 5
task: 6
task: 7
thread start1
task: 8
task: 9
task: 10
task: 11
task: 12
task: 13
task: 14
task: 15
从实例可以看到for循环并没有被固定的线程池阻塞住,也就是说所有的线程task都被提交到了ExecutorService中,查看 Executors.newFixedThreadPool()如下:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
可以看到task被提交都了LinkedBlockingQueue中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面:
- import java.io.IOException;
- import java.util.concurrent.ArrayBlockingQueue;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.ThreadPoolExecutor;
- import java.util.concurrent.TimeUnit;
- public class Test {
- public static void main(String[] args) throws IOException, InterruptedException {
- BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(3);
- ThreadPoolExecutor executor = new ThreadPoolExecutor(3, 3, 1, TimeUnit.HOURS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
- for (int i = 0; i < 10; i++) {
- final int index = i;
- System.out.println("task: " + (index+1));
- Runnable run = new Runnable() {
- @Override
- public void run() {
- System.out.println("thread start" + (index+1));
- try {
- Thread.sleep(Long.MAX_VALUE);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("thread end" + (index+1));
- }
- };
- executor.execute(run);
- }
- }
- }
输出:
task: 1
task: 2
thread start1
task: 3
task: 4
task: 5
task: 6
task: 7
thread start2
thread start7
thread start6
task: 2
thread start1
task: 3
task: 4
task: 5
task: 6
task: 7
thread start2
thread start7
thread start6
线程池最大值为4(??这里我不明白为什么是设置值+1,即3+1,而不是3),准备执行的任务队列为3。可以看到for循环先处理4个task,然后把3个放到队列。这样就实现了自动阻塞队列的效果。记得要使用ArrayBlockingQueue这个队列,然后设置容量就OK了。
1 0
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- Executors.newFixedThreadPool线程池的使用
- android Executors.newFixedThreadPool和newCachedThreadPool区别
- android Executors.newFixedThreadPool和newCachedThreadPool区别
- Executors.newCachedThreadPool();和Executors.newFixedThreadPool(int n)的区别
- Executors.newFixedThreadPool / Executors.newScheduledThreadPool 例子
- 线程池Executors.newFixedThreadPool
- 线程池Executors.newFixedThreadPool
- 线程池Executors.newFixedThreadPool
- android 多线程 - 线程池 Executors.newFixedThreadPool 的使用例子
- android 多线程 - 线程池 Executors.newFixedThreadPool 的使用例子
- java 多线程之Executors.newFixedThreadPool
- 使用Executors和ThreadPoolExecutor2
- ArrayBlockingQueue和LinkedBlockingQueue的使用
- thinkphp+jquery+iframe异步上传
- cocos2d-x 源码分析 总目录
- 为什么要使用ZooKeeper
- LinkIssue: Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or cor
- 的是个负担是格式大哥
- Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
- [总结]视音频技术零基础学习方法
- ZooKeeper 安装、配置
- Mysql 主从同步问题总结
- 黑马程序员_类的一些边缘知识
- 正责表达式匹配多行注释
- JQuery对html控件操作总结
- Android项目用到的一些开源项目
- Android2.1下调试3G模块(三)