1. 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中。这里有个问题,如果任务列表很大,一定会把内存撑爆,如何解决?看下面:

  1. 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了。


转载网址:http://heipark.iteye.com/blog/1393847

更多相关文章

  1. Android通过HTTP协议实现多线程下载
  2. Android(安卓)Service学习之IntentService 深入分析
  3. WebView.onDraw (Android(安卓)4.1)
  4. android 线程方式打印log到sd卡
  5. android Thread和Runnable的区别
  6. Android(安卓)浅析 Volley
  7. Android的GLSurfaceView测试源码
  8. 线程
  9. Android从相机或相册获取图片裁剪

随机推荐

  1. 【原创】窥视懒人的秘密---android下拉刷
  2. Android(安卓)NFC架构分析
  3. android 笔记 --- Android中开发中常用代
  4. android安装配置
  5. Android(安卓)UI开发第五篇——自定义列
  6. TextView
  7. android 布局相关
  8. 2014.01.16 ——— android 调整屏幕分辨
  9. android 动态效果学习之旅
  10. Android(安卓)加载assets中的资源文件实