Executors.newFixedThreadPool和ArrayBlockingQueue一点使用心得
2016-05-11 15:05
591 查看
http://heipark.iteye.com/blog/1393847
newFixedThreadPool内部有个任务队列,假设线程池里有3个线程,提交了5个任务,那么后两个任务就放在任务队列了,即使前3个任务sleep或者堵塞了,也不会执行后两个任务,除非前三个任务有执行完的newFixedThreadPool使用范例: Java代码
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
从实例可以看到for循环并没有被固定的线程池阻塞住,也就是说所有的线程task都被提交到了ExecutorService中,查看 Executors.newFixedThreadPool()如下: public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
可以看到task被提交都了LinkedBlockingQueue中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面: Java代码
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
线程池最大值为4(??这里我不明白为什么是设置值+1,即3+1,而不是3),准备执行的任务队列为3。可以看到for循环先处理4个task,然后把3个放到队列。这样就实现了自动阻塞队列的效果。记得要使用ArrayBlockingQueue这个队列,然后设置容量就OK了。
newFixedThreadPool内部有个任务队列,假设线程池里有3个线程,提交了5个任务,那么后两个任务就放在任务队列了,即使前3个任务sleep或者堵塞了,也不会执行后两个任务,除非前三个任务有执行完的newFixedThreadPool使用范例: Java代码
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
从实例可以看到for循环并没有被固定的线程池阻塞住,也就是说所有的线程task都被提交到了ExecutorService中,查看 Executors.newFixedThreadPool()如下: public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
可以看到task被提交都了LinkedBlockingQueue中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面: Java代码
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
线程池最大值为4(??这里我不明白为什么是设置值+1,即3+1,而不是3),准备执行的任务队列为3。可以看到for循环先处理4个task,然后把3个放到队列。这样就实现了自动阻塞队列的效果。记得要使用ArrayBlockingQueue这个队列,然后设置容量就OK了。
相关文章推荐
- 使用THREAD_POOL.execute引发一次神奇的多线程问题
- iOS开发之基础视图——UILabel
- UITextField 控制文本输入小数点和小数点后几位
- django1.8 提示(1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your defau
- iOS开发 贝塞尔曲线UIBezierPath(后记)
- 学习笔记-UITableView的数据源(dataSource)和代理(delegate)
- iOS开发 贝塞尔曲线UIBezierPath
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
- easyUI中tab中的html片段导包与父页面的包冲突
- 【FLUENT案例】06:与EDEM耦合计算
- 【代码】key-value模式下的哈希二次探测与简单的哈希类的实现
- UE4 性能优化方法(工具篇)
- hdu 3836 Equivalent Sets
- pat 1007. Maximum Subsequence Sum (25)
- 优化duilib中的CDateTimeUI控件-修正XP下无法使用的问题
- Hue 可视化的Hadoop架构工具
- 线程中更新 UILabel的text
- org.hibernate.PropertyAccessException: Null value was assigned to a property。。。。完美解决
- NGUI之UIGrid & UITable
- String、StringBuilder、 StringBuffer 深入分析 源码解析