先讲方案,再讲实现
Tips:下列代码实现效果与计算机自身CPU的计算能力、数据量、线程数相关,本文测试数据仅供参考!
public static void testCycleComputing(){
Random random = new Random();
int[] array = new int[100000000];
Arrays.fill(array, random.nextInt(10000));
long startTime = System.currentTimeMillis();
int result = 0;
for (int i = 0; i < array.length; i++) {
result += array[i];
}
long endTime = System.currentTimeMillis();
System.out.printf("总计为:%d ,总耗时:%s ms",result,(endTime - startTime));
}
结果耗时如下:
总计为:2127125760 ,总耗时:67 ms
public static void testThreadCal(){
Random random = new Random();
int[] array = new int[100000000];
Arrays.fill(array, random.nextInt(10000));
CalThread[] threads = new CalThread[4];
int numThreads = 2;
int blockSize = array.length / numThreads; // 每个线程处理的子数组大小
long startTime = System.currentTimeMillis();
// 创建并启动线程
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize;
int end = (i == numThreads - 1) ? array.length : start + blockSize;
threads[i] = new CalThread(array, start, end);
threads[i].start();
}
int result = 0;
// 等待所有线程执行完毕,并累加各个子数组的和
for (int i = 0; i < numThreads; i++) {
try {
threads[i].join();
result += threads[i].getPartialSum();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.printf("总计为:%d ,总耗时:%s ms",result,(endTime - startTime));
}
结果耗时如下:
总计为:-767641600 ,总耗时:46 ms
public static void testParallel(){
Random random = new Random();
int[] array = new int[100000000];
Arrays.fill(array, random.nextInt(10000));
long startTime = System.currentTimeMillis();
int result = Arrays.stream(array).parallel().sum();
long endTime = System.currentTimeMillis();
System.out.printf("总计为:%d ,总耗时:%s ms",result,(endTime - startTime));
}
结果耗时如下:
总计为:1305686784 ,总耗时:116 ms
import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class SumArrayExample {
private static final int THRESHOLD = 100000; // 阈值
public static void main(String[] args) {
int[] arr = new int[100000000];
Random random = new Random();
Arrays.fill(arr, random.nextInt(10000));
ForkJoinPool pool = new ForkJoinPool();
long startTime = System.currentTimeMillis();
int result = pool.invoke(new SumArrayTask(arr, 0, arr.length)); // 执行任务
long endTime = System.currentTimeMillis();
System.out.printf("总计为:%d ,总耗时:%s ms",result,(endTime - startTime));
}
static class SumArrayTask extends RecursiveTask<Integer> {
private int[] arr;
private int start;
private int end;
public SumArrayTask(int[] arr, int start, int end) {
this.arr = arr;
this.start = start;
this.end = end;
}
@Override
protected Integer compute() {
if (end - start <= THRESHOLD) { // 如果数据量小于阈值,直接求和
int sum = 0;
for (int i = start; i < end; i++) {
sum += arr[i];
}
return sum;
} else { // 如果数据量较大,将任务分解为子任务
int mid = (start + end) / 2;
SumArrayTask leftTask = new SumArrayTask(arr, start, mid);
SumArrayTask rightTask = new SumArrayTask(arr, mid, end);
leftTask.fork();
rightTask.fork();
return leftTask.join() + rightTask.join();
}
}
}
}
首先生成了一个长度为 100000000 的 int 数组,将其随机填充。然后创建了 Fork/Join 线程池,并在主线程中调用 pool.invoke() 方法执行任务。
在 SumArrayTask 类中,我们判断当前任务的数据量是否小于阈值,如果小于阈值,则直接求和;否则,将任务分解为两个子任务,分别计算并返回结果。
结果耗时如下:
总计为:-386664192 ,总耗时:50 ms
像面试这种编码题时不用急着想最优解,面试官更多的是看你了解的广度是否达到他的预期!