gpt4 book ai didi

java - 在Java并发教程的fork/join部分中, "computeDirectly"函数是如何通过 "invokeAll"执行的?

转载 作者:行者123 更新时间:2023-11-30 03:31:06 25 4
gpt4 key购买 nike

the fork/join chapter在官方的Java并发教程中,有以下部分示例代码。

我知道如果图像数组大小小于阈值(100,000),则直接计算(computeDirectly)。否则,它将被分成两半,并创建并调用两个 ForkBlur 对象 (invokeAll)。我不明白的是 invokeAll 最终如何为这些片段执行 computeDirectly 函数。

如何为大于阈值的数组调用computeDirectly函数? (它被分成几部分。)

public class ForkBlur extends RecursiveAction {
private int[] mSource;
private int mStart;
private int mLength;
private int[] mDestination;

// Processing window size; should be odd.
private int mBlurWidth = 15;

public ForkBlur(int[] src, int start, int length, int[] dst) {
mSource = src;
mStart = start;
mLength = length;
mDestination = dst;
}

protected void computeDirectly() {
int sidePixels = (mBlurWidth - 1) / 2;
for (int index = mStart; index < mStart + mLength; index++) {
// Calculate average.
float rt = 0, gt = 0, bt = 0;
for (int mi = -sidePixels; mi <= sidePixels; mi++) {
int mindex = Math.min(Math.max(mi + index, 0),
mSource.length - 1);
int pixel = mSource[mindex];
rt += (float)((pixel & 0x00ff0000) >> 16)
/ mBlurWidth;
gt += (float)((pixel & 0x0000ff00) >> 8)
/ mBlurWidth;
bt += (float)((pixel & 0x000000ff) >> 0)
/ mBlurWidth;
}

// Reassemble destination pixel.
int dpixel = (0xff000000 ) |
(((int)rt) << 16) |
(((int)gt) << 8) |
(((int)bt) << 0);
mDestination[index] = dpixel;
}
}

...

这个单独的部分称它为:

protected static int sThreshold = 100000;

protected void compute() {
if (mLength < sThreshold) {
computeDirectly();
return;
}

int split = mLength / 2;

invokeAll(new ForkBlur(mSource, mStart, split, mDestination),
new ForkBlur(mSource, mStart + split, mLength - split,
mDestination));
}

最佳答案

这个

invokeAll(new ForkBlur(mSource, mStart, split, mDestination),
new ForkBlur(mSource, mStart + split, mLength - split,
mDestination));

最终调用 ForkBlurcompute 方法,如果您将其分割为足够小的阈值,则将执行 computeDirectly在此处传递 if 条件

if (mLength < sThreshold) {
computeDirectly();
return;
}

因此,一个大任务被拆分为两个(或更多)较小的任务,这些任务可能会一次又一次地拆分,直到任务小到足以运行为止。

关于java - 在Java并发教程的fork/join部分中, "computeDirectly"函数是如何通过 "invokeAll"执行的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29017684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com