场景
用分支/合并框架执行并行求和,通过Junit进行测试。
package java8.chapter07.demo01;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;
/**
* @description
* @date:2022/10/28 22:52
* @author: qyl
*/
public class ForkJoinSumCalculator extends RecursiveTask<Long> { // 继 承 RecursiveTask 来创建可以用于分支/合并框架的任务
private final long[] numbers;
private final int start;
private final int end;
private static final long THRESHOLD = 10000;
public ForkJoinSumCalculator(long[] numbers) {
this(numbers, 0, numbers.length);
}
public ForkJoinSumCalculator(long[] numbers, int start, int end) {
this.numbers = numbers;
this.start = start;
this.end = end;
}
// 该任务负责求和的部分的大小
@Override
protected Long compute() {
int length = end - start;
if (length <= THRESHOLD) {
return computeSequentially(); // 小于阈值 顺序执行
}
ForkJoinSumCalculator leftTask = new ForkJoinSumCalculator(numbers, start, start + length / 2);
// 计算左边数组的任务放入当前线程池
leftTask.fork();
ForkJoinSumCalculator rightTask = new ForkJoinSumCalculator(numbers, start + length / 2, end);
// 创建一个任务为数组的后一半求和
Long rightResult = rightTask.compute();
Long leftResult = leftTask.join();
return rightResult + leftResult;
}
private long computeSequentially() {
long sum = 0 ;
for (int i = start ;i < end;i++){
sum += numbers[i];
}
return sum;
}
@Test
public void test(){
long[] nums = LongStream.rangeClosed(1,100000).toArray();
ForkJoinTask<Long> task = new ForkJoinSumCalculator(nums);
System.out.println(new ForkJoinPool().invoke(task));
}
}
出现了如下所示报错:
问题排查
明确说了必须只能声明一个单一的构造器,这里确实我们使用了多个有参构造器,加上默认的无参构造器,其实有多个构造器的重载,不妨移动test()方法到一个新的类中。
package java8.chapter07.demo01;
import org.junit.jupiter.api.Test;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;
/**
* @description
* @date:2022/10/28 22:52
* @author: qyl
*/
public class ForkJoinSumCalculator extends RecursiveTask<Long> { // 继 承 RecursiveTask 来创建可以用于分支/合并框架的任务
private final long[] numbers;
private final int start;
private final int end;
private static final long THRESHOLD = 10000;
public ForkJoinSumCalculator(long[] numbers) {
this(numbers, 0, numbers.length);
}
public ForkJoinSumCalculator(long[] numbers, int start, int end) {
this.numbers = numbers;
this.start = start;
this.end = end;
}
// 该任务负责求和的部分的大小
@Override
protected Long compute() {
int length = end - start;
if (length <= THRESHOLD) {
return computeSequentially(); // 小于阈值 顺序执行
}
ForkJoinSumCalculator leftTask = new ForkJoinSumCalculator(numbers, start, start + length / 2);
// 计算左边数组的任务放入当前线程池
leftTask.fork();
ForkJoinSumCalculator rightTask = new ForkJoinSumCalculator(numbers, start + length / 2, end);
// 创建一个任务为数组的后一半求和
Long rightResult = rightTask.compute();
Long leftResult = leftTask.join();
return rightResult + leftResult;
}
private long computeSequentially() {
long sum = 0 ;
for (int i = start ;i < end;i++){
sum += numbers[i];
}
return sum;
}
}
class MyTest{
@org.junit.jupiter.api.Test
public void test(){
long[] nums = LongStream.rangeClosed(1,100000).toArray();
ForkJoinTask<Long> task = new ForkJoinSumCalculator(nums);
System.out.println(new ForkJoinPool().invoke(task));
}
}
再次运行,结果如下:
文章来源:https://www.toymoban.com/news/detail-505872.html
总结
通过Junit进行测试的时候,包含了@Test标注的测试方法所在的类不能包含多个构造器的重载,只能存在单一的无参构造器。文章来源地址https://www.toymoban.com/news/detail-505872.html
到了这里,关于org.junit.platform.commons.PreconditionViolationException: Class [java8.chapter07.demo01.ForkJoinSum的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!