ExecutorService

public class CompletionServiceTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
       ExecutorService pool = Executors.newFixedThreadPool(10);
       CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(pool);
       for(int i=1;i<=10;i++){
    	   final int seq = i;
    	   completionService.submit(new Callable<Integer>(){

   			public Integer call() throws Exception {
   				// TODO Auto-generated method stub
   				return seq;
   			}
       	   
          });
       }
       for(int i=0;i<10;i++){
    	   try {//等着菜熟,先熟,先收貨
			System.out.println(completionService.take().get());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       }
	}

}
相關文章
相關標籤/搜索