gpt4 book ai didi

java - ForkBlur.compute() 方法是在哪里以及如何调用的?

转载 作者:行者123 更新时间:2023-12-02 03:53:41 26 4
gpt4 key购买 nike

我在Java教程oracle中看到了下面的代码。我的问题是,如何以及在何处调用 ForkBlur.compute() 方法?我在任何地方都没有看到任何对 ForkBlur.compute() 的调用,但该方法确实被执行了。我有什么遗漏的吗?

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import javax.imageio.ImageIO;

/**
* ForkBlur implements a simple horizontal image blur. It averages pixels in the
* source array and writes them to a destination array. The sThreshold value
* determines whether the blurring will be performed directly or split into two
* tasks.
*
* This is not the recommended way to blur images; it is only intended to
* illustrate the use of the Fork/Join framework.
*/
public class ForkBlur extends RecursiveAction {

private int[] mSource;
private int mStart;
private int mLength;
private int[] mDestination;
private int mBlurWidth = 15; // Processing window size, should be odd.

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

// Average pixels from source, write results into destination.
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;
}

// Re-assemble destination pixel.
int dpixel = (0xff000000)
| (((int) rt) << 16)
| (((int) gt) << 8)
| (((int) bt) << 0);
mDestination[index] = dpixel;
}
}
protected static int sThreshold = 10000;

@Override
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));
}

// Plumbing follows.
public static void main(String[] args) throws Exception {
String srcName = "/Users/justin/NetBeansProjects/JavaTutorialOracle/src/JTOConcurrency/Screen Shot 2015-12-28 at 10.45.31 PM.jpg";
File srcFile = new File(srcName);
BufferedImage image = ImageIO.read(srcFile);

System.out.println("Source image: " + srcName);

BufferedImage blurredImage = blur(image);

String dstName = "blurred-tulips.jpg";
File dstFile = new File(dstName);
ImageIO.write(blurredImage, "jpg", dstFile);

System.out.println("Output image: " + dstName);

}

public static BufferedImage blur(BufferedImage srcImage) {
int w = srcImage.getWidth();
System.out.println("w: " + w);
int h = srcImage.getHeight();
System.out.println("h: " + h);

int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w);

System.out.println("Array size is " + src.length);
System.out.println("Threshold is " + sThreshold);

int processors = Runtime.getRuntime().availableProcessors();
System.out.println(Integer.toString(processors) + " processor"
+ (processors != 1 ? "s are " : " is ")
+ "available");

ForkBlur fb = new ForkBlur(src, 0, src.length, dst);

ForkJoinPool pool = new ForkJoinPool();

long startTime = System.currentTimeMillis();
pool.invoke(fb);

long endTime = System.currentTimeMillis();

System.out.println("Image blur took " + (endTime - startTime) +
" milliseconds.");

BufferedImage dstImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
dstImage.setRGB(0, 0, w, h, dst, 0, w);

return dstImage;
}
}

最佳答案

Forkblur 正在扩展 java.util.concurrent.RecursiveAction,其中包含方法 compute() 并将其描述为“此任务执行的主要计算”。基本上发生的情况如下:

当你调用ForkJoinPool.invoke()并指定要执行的任务时(在本例中ForkBlur是一个ForkJoinTask实现),JAVA SDK实际上启动了一个编号线程,每个线程以并行方式执行指示任务。实际要执行的操作在compute()方法中定义。因此,该方法总是由JAVA SDK调用,而不是由程序员调用。

关于java - ForkBlur.compute() 方法是在哪里以及如何调用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668940/

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