`
bugyun
  • 浏览: 568320 次
社区版块
存档分类
最新评论

CompletionService实例-Mine

 
阅读更多
package com.bugyun.test;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyCompletionService {
	
	private ExecutorService executorService;
	private CompletionService<Long> completionService;
	private int cpuCoreNumber;

	public MyCompletionService() {
//		获取当前系统的CPU 数目
		cpuCoreNumber = Runtime.getRuntime().availableProcessors();
//		ExecutorService通常根据CPU数目定义线程池大小
		executorService = Executors.newFixedThreadPool(cpuCoreNumber);
//		更高级的ExecutorService
		completionService = new ExecutorCompletionService<Long>(executorService);
	}
	
	
	/**
	 * 将任务分配给子任务执行
	 */
	public Long statistic(final int[] numbers) {
		int size = numbers.length;
		for (int i = 0; i < cpuCoreNumber; i++) {
			
			int increment = size / cpuCoreNumber + 1;
			int start = increment * i;
			int end = increment * (i + 1);
			if (end > size){
				end = size;
			}
			Task task = new Task(numbers, start, end);
			if(!executorService.isShutdown()){
				completionService.submit(task);
			}
		}
		return getResult();
	}

	/**
	 * 遍历子任务,获取结果
	 */
	public Long getResult() {
		Long result = 0l;
		for (int i = 0; i < cpuCoreNumber; i++) {
			try {
				Long subSum = completionService.take().get();
				System.out.println(" ===> 获取结果的核是:"+i+" , 此时结果为:"+subSum);
				result += subSum;
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		return result;
	}

	public void close() {
		executorService.shutdown();
	}

	
	class Task implements Callable<Long> {
		private int[] numbers;
		private int start;
		private int end;

		public Task(final int[] numbers, int start, int end) {
			this.numbers = numbers;
			this.start = start;
			this.end = end;
		}

		public Long call() throws Exception {
			Long sum = 0l;
			for (int i = start; i < end; i++) {
				sum += numbers[i];
			}
			return sum;
		}
	}
	
	
	public static void main(String... args) throws Exception {
		int[] numbers = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
		MyCompletionService myCompletion = new MyCompletionService();
		Long result = myCompletion.statistic(numbers);
		System.out.println("最终结果是:"+result);
		myCompletion.close();
	}

}

 

 运行结果:


 

  • 大小: 15.5 KB
分享到:
评论

相关推荐

    Python 实例 - 桌面宠物 Python源码

    Python 实例 - 桌面宠物 Python源码Python 实例 - 桌面宠物 Python源码Python 实例 - 桌面宠物 Python源码Python 实例 - 桌面宠物 Python源码Python 实例 - 桌面宠物 Python源码Python 实例 - 桌面宠物 Python源码...

    网页设计项目实例-杭州旅游网页(源码)

    网页设计项目实例-杭州旅游网页(源码)网页设计项目实例-杭州旅游网页(源码)网页设计项目实例-杭州旅游网页(源码)网页设计项目实例-杭州旅游网页(源码)网页设计项目实例-杭州旅游网页(源码)网页设计项目...

    MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar

    MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar MapReduce Java API实例-统计单次出现频率示例代码-MapReduceDemo.rar

    Python 实例 - 根据IP地址查对应的地理信息 Python源码

    Python 实例 - 根据IP地址查对应的地理信息 Python源码Python 实例 - 根据IP地址查对应的地理信息 Python源码Python 实例 - 根据IP地址查对应的地理信息 Python源码Python 实例 - 根据IP地址查对应的地理信息 Python...

    Python 实例 - 贺卡 Python源码

    Python 实例 - 贺卡 Python源码Python 实例 - 贺卡 Python源码Python 实例 - 贺卡 Python源码Python 实例 - 贺卡 Python源码Python 实例 - 贺卡 Python源码Python 实例 - 贺卡 Python源码Python 实例 - 贺卡 Python...

    Python 实例 - 画画 Python源码

    Python 实例 - 画画 Python源码Python 实例 - 画画 Python源码Python 实例 - 画画 Python源码Python 实例 - 画画 Python源码Python 实例 - 画画 Python源码Python 实例 - 画画 Python源码Python 实例 - 画画 Python...

    Python 实例 - 截图工具 Python源码

    Python 实例 - 截图工具 Python源码Python 实例 - 截图工具 Python源码Python 实例 - 截图工具 Python源码Python 实例 - 截图工具 Python源码Python 实例 - 截图工具 Python源码Python 实例 - 截图工具 Python源码...

    Python 实例 - 五星红旗 Python源码

    Python 实例 - 五星红旗 Python源码Python 实例 - 五星红旗 Python源码Python 实例 - 五星红旗 Python源码Python 实例 - 五星红旗 Python源码Python 实例 - 五星红旗 Python源码Python 实例 - 五星红旗 Python源码...

    Python 实例 - 快递查询 Python源码

    Python 实例 - 快递查询 Python源码Python 实例 - 快递查询 Python源码Python 实例 - 快递查询 Python源码Python 实例 - 快递查询 Python源码Python 实例 - 快递查询 Python源码Python 实例 - 快递查询 Python源码...

    Python 实例 - 翻译软件 Python源码

    Python 实例 - 翻译软件 Python源码Python 实例 - 翻译软件 Python源码Python 实例 - 翻译软件 Python源码Python 实例 - 翻译软件 Python源码Python 实例 - 翻译软件 Python源码Python 实例 - 翻译软件 Python源码...

    Python 实例 - 音乐播放器 Python源码

    Python 实例 - 音乐播放器 Python源码Python 实例 - 音乐播放器 Python源码Python 实例 - 音乐播放器 Python源码Python 实例 - 音乐播放器 Python源码Python 实例 - 音乐播放器 Python源码Python 实例 - 音乐播放器 ...

    Python 实例 - 画爱心 Python源码

    Python 实例 - 画爱心 Python源码Python 实例 - 画爱心 Python源码Python 实例 - 画爱心 Python源码Python 实例 - 画爱心 Python源码Python 实例 - 画爱心 Python源码Python 实例 - 画爱心 Python源码Python 实例 - ...

    Python 实例 - personal-qrcode个性二维码 Python源码

    Python 实例 - personal-qrcode个性二维码 Python源码Python 实例 - personal-qrcode个性二维码 Python源码Python 实例 - personal-qrcode个性二维码 Python源码Python 实例 - personal-qrcode个性二维码 Python源码...

    计算机导论第5章办公软件应用素材和实例-excel实例-食品公司销售情况(结果).pdf

    计算机导论第5章办公软件应用素材和实例-excel实例-食品公司销售情况(结果)

    Python 实例 - 查票抢票 Python源码

    Python 实例 - 查票抢票 Python源码Python 实例 - 查票抢票 Python源码Python 实例 - 查票抢票 Python源码Python 实例 - 查票抢票 Python源码Python 实例 - 查票抢票 Python源码Python 实例 - 查票抢票 Python源码...

    Python 实例 - 抖音表白2 Python源码

    Python 实例 - 抖音表白2 Python源码Python 实例 - 抖音表白2 Python源码Python 实例 - 抖音表白2 Python源码Python 实例 - 抖音表白2 Python源码Python 实例 - 抖音表白2 Python源码Python 实例 - 抖音表白2 Python...

    Python 实例 - 简单计时器 Python源码

    Python 实例 - 简单计时器 Python源码Python 实例 - 简单计时器 Python源码Python 实例 - 简单计时器 Python源码Python 实例 - 简单计时器 Python源码Python 实例 - 简单计时器 Python源码Python 实例 - 简单计时器 ...

    Python 实例 - 屏保计时器 Python源码

    Python 实例 - 屏保计时器 Python源码Python 实例 - 屏保计时器 Python源码Python 实例 - 屏保计时器 Python源码Python 实例 - 屏保计时器 Python源码Python 实例 - 屏保计时器 Python源码Python 实例 - 屏保计时器 ...

    Python 实例 - 二维码生成器 Python源码

    Python 实例 - 二维码生成器 Python源码Python 实例 - 二维码生成器 Python源码Python 实例 - 二维码生成器 Python源码Python 实例 - 二维码生成器 Python源码Python 实例 - 二维码生成器 Python源码Python 实例 - ...

    100套python项目实例代码(工具+游戏+办公).zip

    实例-01 图片识别 实例-02 代码雨 实例-03 幸运大转盘 实例-04 简易时钟 实例-05 查票抢票 实例-07 抖音表白 实例-08 抖音表白 实例-09 二维码生成器 实例-10 翻译软件 实例-11根据IP地址查对应的地理信息 实例-12 ...

Global site tag (gtag.js) - Google Analytics