ArrayBlockingQueue和ExecutorService的理解与感悟
2015-06-02 10:59
232 查看
package test; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; public class Test { public static void main(String[] args) { //executorServiceTest() arrayBlockingQueueTest(); } public static void arrayBlockingQueueTest(){ BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(5); ThreadPoolExecutor executor=new ThreadPoolExecutor(3, Runtime.getRuntime().availableProcessors(), 1, TimeUnit.HOURS, queue,new ThreadPoolExecutor.CallerRunsPolicy()); for(int i=0;i<100;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(1000*3); }catch(Exception e){ e.printStackTrace(); } System.out.println("thread end "+index); } }; executor.execute(run); } } public static void executorServiceTest(){ ExecutorService service = Executors.newFixedThreadPool(2); for(int i=0;i<100;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(1000*3); }catch(Exception e){ e.printStackTrace(); } System.out.println("thread end "+index); } }; service.execute(run); } } }
调用executorServiceTest方法的运行结果
task:1 task:2 task:3 thread start0 task:4 task:5 thread start1 task:6 task:7 task:8 task:9 task:10 thread end 0 thread start2 thread end 1 thread start3 thread end 2 thread end 3 thread start4 thread start5 thread end 5 thread start6 thread end 4 thread start7 thread end 6 thread end 7 thread start8 thread start9 thread end 8 thread end 9
调用arrayBlockingQueueTest运行结果
task:1 task:2 task:3 thread start0 thread start1 task:4 thread start2 task:5 task:6 task:7 thread start6 thread end 1 thread end 6 thread end 0 thread end 2 thread start5 thread start4 task:8 task:9 thread start3 task:10 thread end 5 thread end 3 thread end 4 thread start9 thread start8 thread start7 thread end 9 thread end 8 thread end 7
从运行的结果看,不难看出用ExecutorService的话是将所有的线程调用起来,放在内存中。
而ArrayBlockingQueue的话是线程有个容量,从运行结果看是4个,这样实现了自动阻塞队列的效果。
相关文章推荐
- iOS学习(1)——UIButton
- 优化UITableViewCell高度计算的那些事
- 后台request获得所有参数(当你不知道它包含什么参数)
- UIImage 按照指定宽度等比例裁切
- GRE ISSUE写作论证论据素材 国家制度建设类
- UIKit 框架之UISearchController
- APUE: Ext2文件系统布局,文件数据块寻址,VFS虚拟文件系统
- poj2478 Farey Sequence(线性筛法+欧拉函数+递推)
- Arch下Android Studio下报错Cannot run program "/xx/build-tools/xx/aapt": error=2解决
- LABjs、RequireJS、SeaJS的区别
- Entity Framework:使用Fluent API配置数据表映射
- Entity Framework:IQueryable与IEnumberable的区别
- 使用Data Annotations和Fluent API配置数据库的映射
- ThreadLocal 与 Request 和 Session 之关联
- vlc-android-macbuild
- Fluent NHibernate
- Permutation Sequence
- UIView如何管理它的子视图
- FlashBuilder 4.6 破解
- FCKEditor使用RequiredFieldValidator验证时必须点击两次的解决办法