读书笔记:排序的执行器

package v1ch14.ThreadPoolTest;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @version 2010-10-18 下午05:11:11
 * @author DaWei han
 * @EMAIL  handawei_1@126.com
 */
public class MyPoolTest {

	/**
	 * @param args
	 * @throws ExecutionException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		//构造一个线程池
		ExecutorService executor=Executors.newCachedThreadPool();
		
		//构造一个顺序的执行器
		ExecutorCompletionService<Integer> service=new ExecutorCompletionService<Integer>(executor);
		List<Callable<Integer>> taks=getTasks();
		
		for(Callable<Integer> call:taks)
		{
			//执行任务
			service.submit(call);
		}
		for(int i=0;i<taks.size();i++)
		{
			//获得任务结果。它们是按结果的快慢排序的。
		    service.take().get(); 	
		}
      //上面的线程池顺序服务是为了优化下面的线程池服务,因为下面的第一个任务可能很耗时,那么后面的就必须等待。
		List<Future<Integer>> results= executor.invokeAll(taks);
		for(Future<Integer> result:results)
		{
			result.get();
		}
	}
 public static List<Callable<Integer>> getTasks()
 {
	 return null;
 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值