gpt4 book ai didi

java - 为什么 Future isDone() 会像 Future get() 一样阻塞程序?

转载 作者:行者123 更新时间:2023-11-30 08:39:32 24 4
gpt4 key购买 nike

我正在编写一个生成分形和分形动画的程序,问题出在动画上......

我有一个生成 List<Callable<Long>> tasks 的主线程每个元素都有生成一个框架的信息和资源;然后我使用 ExecutorService提交作品。

问题在于,如果用户想要停止微积分,则无法取消这些辅助线程。这是代码:

public class Animation extends Task<Long> {
protected Long call() throws Exception {
long startTime = System.currentTimeMillis();

WritableImage[] frames = new WritableImage[frameNumber];
List<Callable<Long>> tasks = new ArrayList<>();

updateProgress(count.incrementAndGet(), maxCount);
if (isCancelled()) {
return System.currentTimeMillis() - startTime;;
}

for (int k = 0; k < frameNumber; k++) {
frames[k] = new WritableImage(
(int) start.getCartesianPlane().getWidth(),
(int) start.getCartesianPlane().getHeight());

CartesianFractal tmp = FractalFactory.bulidFractal(
selectedFractal, nextDataBox(k), colorPalette);

tmp.setOnFinish(t -> {
updateProgress(count.incrementAndGet(), maxCount);
return null;
});

tasks.add((Callable<Long>) tmp);
if (isCancelled()) {
return System.currentTimeMillis() - startTime;;
}
}

executor = Executors.newFixedThreadPool(4);
updateProgress(count.incrementAndGet(), maxCount);
if (isCancelled()) {
return System.currentTimeMillis() - startTime;
}
try {
result = executor.invokeAll(tasks);
}
catch (InterruptedException ex) {
System.err.println(ex.toString());
}

// Check if all tasks are finished
boolean finished = false;
while (!finished) {
finished = true;
// Check if it is all done
for (Future<Long> r : result) {
finished = finished && r.isDone(); // THE PROGRAM BLOCKS HERE
// Check if the task was cancelled
if (isCancelled()) {
// Cancell all task
tasks.stream().forEach((t) -> {
((CartesianFractal)t).myCancel();
});
// Turnoff the executor
executor.shutdown();
return System.currentTimeMillis() - startTime;
}
}
}

// Turnoff the executor
executor.shutdown();
updateProgress(count.incrementAndGet(), maxCount);

makeAnimation();
updateProgress(count.incrementAndGet(), maxCount);

return System.currentTimeMillis() - startTime;
}
}

我真的不明白为什么Future.isDone()阻止像Future.get()这样的程序!

这是我的第一个问题,所以我希望一切都好。

最佳答案

我认为如果您使用 CompletionService 实现它可能会更容易,它会按完成的顺序返回您的 Future

例如:

Executor executor = Executors.newFixedThreadPool(4);

try {
CompletionService completionService = new ExecutorCompletionService(executor);

List<Future<Long>> futures = new ArrayList<>();
for (Callable<Long> task : task) {
futures.add(completionService.submit(task));
}

int pending = futures.size();
while (pending > 0) {
// Wait for up to 100ms to see if anything has completed.
// The completed future is returned if one is found; otherwise null.
// (Tune 100ms as desired)
Future<Long> completed = completionService.poll(100, TimeUnit.MILLISECONDS);
if (completed != null) {
updateProgress(count.incrementAndGet(), maxCount);
--pending;
}
if (isCancelled()) {
// Cancel all task etc.
break;
}
}
} finally {
executor.shutdown();
}

关于java - 为什么 Future isDone() 会像 Future get() 一样阻塞程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094887/

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