java多線程類比並行計算架構

來源:互聯網
上載者:User
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;}@Overridepublic 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;}@Overridepublic 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);}}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.