你的位置:首页 > 软件开发 > Java > newFixedThreadPool

newFixedThreadPool

发布时间:2015-08-03 19:00:08
newFixedThreadPool内部有个任务队列,假设线程池里有3个线程,提交了5个任务,那么后两个任务就放在任务队列了,即使前3个任务sleep或者堵塞了,也不会执行后两个任务,除非前三个任务有执行完的 newFixedThreadPool使用范例: J ...

 

newFixedThreadPool内部有个任务队列,假设线程池里有3个线程,提交了5个任务,那么后两个任务就放在任务队列了,即使前3个任务sleep或者堵塞了,也不会执行后两个任务,除非前三个任务有执行完的

 

newFixedThreadPool使用范例:

 

 

 

Java代码  newFixedThreadPool
  1. import java.io.IOException;  
  2. import java.util.concurrent.ExecutorService;  
  3. import java.util.concurrent.Executors;  
  4.   
  5. public class Test {  
  6.   
  7.     public static void main(String[] args) throws IOException, InterruptedException {  
  8.         ExecutorService service = Executors.newFixedThreadPool(2);  
  9.         for (int i = 0; i < 6; i++) {  
  10.             final int index = i;  
  11.             System.out.println("task: " + (i+1));  
  12.             Runnable run = new Runnable() {  
  13.                 @Override  
  14.                 public void run() {  
  15.                     System.out.println("thread start" + index);  
  16.                     try {  
  17.                         Thread.sleep(Long.MAX_VALUE);  
  18.                     } catch (InterruptedException e) {  
  19.                         e.printStackTrace();  
  20.                     }  
  21.                     System.out.println("thread end" + index);  
  22.                 }  
  23.             };  
  24.             service.execute(run);  
  25.         }  
  26.     }  
  27. }  

原标题:newFixedThreadPool

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录