java多线程模拟并行计算框架
2012-01-05 22:33
411 查看
package concurrent; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; //用十个线程计算一个数组的和 class TaskWithResult implements Callable<Integer> { private Integer[] ints; public TaskWithResult(Integer[] ints) { this.ints = ints; } private int sumFromArray() { int result = 0; for (int a : this.ints) { result += a; } return result; } @Override public Integer call() throws Exception { return sumFromArray(); } } public class Paiallel1 { public static Integer[] segArray(int start, int end, int[] array) { List<Integer> result = new ArrayList<Integer>(); for (int i = start; i < end; i++) { result.add(array[i]); } Integer[] ary = result.toArray(new Integer[result.size()]); return ary; } public static int getSum(int[] array) { int result = 0; for (int i = 0; i < array.length; i++) { result += array[i]; } return result; } public static void main(String[] args) throws InterruptedException, ExecutionException { long start1 = System.currentTimeMillis(); ExecutorService exec = Executors.newFixedThreadPool(10); int[] array = { 300, 800, 89, 390, 892, 9384, 909, 1, 343, 5839, 939, 43, 355, 323, 32, 55, 3, 3, 43, 5, 5, 45, 555, 554, 554, 555, 545, 555, 553, 35, 2322, 332, 3232, 433, 344, 524, 245, 524, 6565, 526 }; List<FutureTask<Integer>> tasks = new ArrayList<FutureTask<Integer>>(); for (int i = 0; i < 10; i++) { int incre = array.length / 10; int start = i * incre; int end = (i + 1) * incre; if (end > array.length) end = array.length; Integer[] prt = segArray(start, end, array); TaskWithResult calbTask = new TaskWithResult(prt); FutureTask<Integer> task = new FutureTask<Integer>(calbTask); tasks.add(task); if (!exec.isShutdown()) { exec.submit(task); } } int result = 0; for (FutureTask<Integer> task : tasks) { int partRst = task.get(); result += partRst; } exec.shutdown(); long end1 = System.currentTimeMillis(); System.out.println("并行计算耗时:" + (end1 - start1)); System.out.println("并行计算的结果:" + result); long start2 = System.currentTimeMillis(); int result2 = getSum(array); long end2 = System.currentTimeMillis(); System.out.println("并行计算耗时:" + (end2 - start2)); System.out.println("单独计算的结果:" + result2); } }
package concurrent; import java.util.ArrayList; import java.util.List; 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; //用十个线程计算一个数组的和 class TaskWithResult1 implements Callable<Integer> { private Integer[] ints; public TaskWithResult1(Integer[] ints) { this.ints = ints; } private int sumFromArray() { int result = 0; for (int a : this.ints) { result += a; } return result; } @Override public Integer call() throws Exception { return sumFromArray(); } } public class Paiallel2 { public static Integer[] segArray(int start, int end, int[] array) { List<Integer> result = new ArrayList<Integer>(); for (int i = start; i < end; i++) { result.add(array[i]); } Integer[] ary = result.toArray(new Integer[result.size()]); return ary; } public static int getSum(int[] array) { int result = 0; for (int i = 0; i < array.length; i++) { result += array[i]; } return result; } public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService exec = Executors.newFixedThreadPool(10); CompletionService<Integer> cplSvc = new ExecutorCompletionService<Integer>( exec); int[] array = { 300, 800, 89, 390, 892, 9384, 909, 1, 343, 5839, 939, 43, 355, 323, 32, 55, 3, 3, 43, 5, 5, 45, 555, 554, 554, 555, 545, 555, 553, 35, 2322, 332, 3232, 433, 344, 524, 245, 524, 6565, 526 }; for (int i = 0; i < 10; i++) { int incre = array.length / 10; int start = i * incre; int end = (i + 1) * incre; if (end > array.length) end = array.length; Integer[] prt = segArray(start, end, array); TaskWithResult1 calbTask = new TaskWithResult1(prt); if (!exec.isShutdown()) { cplSvc.submit(calbTask); } } int result = 0; for (int i = 0; i < 10; i++) { int partRst = cplSvc.take().get(); result += partRst; } exec.shutdown(); System.out.println("并行计算的结果:" + result); int result2 = getSum(array); System.out.println("单独计算的结果:" + result2); } }
相关文章推荐
- Gearman 并行分布式计算框架
- Tiny并行计算框架之复杂示例
- dispy,asyncoro实现的分布式并行计算框架
- lucene+hadoop 分布式并行计算搜索框架
- 多线程技术模拟并行计算之一:查找极大独立集(Find the Maximal Independent Set)
- 『科学计算』深度学习框架_模拟数据拟合
- Java 7 Fork/Join 并行计算框架概览
- Fourinone创始人彭渊:打造多合一的分布式并行计算框架
- 分布式文件系统,NoSQL数据库,并行计算框架
- 利用MapReduce计算框架实现谷歌(PR值)PageRank算法并行实现
- 并行计算框架 ParallelPython
- 多机并行计算框架 和CoolHash数据库(可用于多机机器学习)
- Java线程(十一):Fork/Join-Java并行计算框架
- Java线程(十一):Fork/Join-Java并行计算框架
- Nulta: Lucene+Hadoop分布式并行计算搜索框架
- 【转载】CUDA并行计算框架
- Spark——并行计算框架
- dispy,asyncoro实现的分布式并行计算框架
- dispy,asyncoro实现的分布式并行计算框架