java计算框架_java多线程模拟并行计算框架

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 {

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 result = new ArrayList();

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> tasks = new ArrayList>();

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 task = new FutureTask(calbTask);

tasks.add(task);

if (!exec.isShutdown()) {

exec.submit(task);

}

}

int result = 0;

for (FutureTask 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 {

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 result = new ArrayList();

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 cplSvc = new ExecutorCompletionService(

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值