Java并发编程 - 无尽的循环

发布于 2025-02-03 08:51:56 字数 1910 浏览 3 评论 0原文

我正在阅读本书介绍游戏框架:Java Web应用程序开发(ISBN 978-1-4842-5645-9),在callable

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableClient {
    /**
     * @param args
     */

    // Step1 : Create a Runnable
    public static void main(String[] args) {
        Callable callableTask = new CallableTask();
        // Step 2: Configure Executor
        // Uses FixedThreadPool executor
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<String> future = executor.submit(callableTask);
        boolean listen = true;
        while (listen) {
            if (future.isDone()) {
                String result;
                try {
                    result = future.get();
                    listen = false;
                    System.out.println(result);
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
        executor.shutdown();
    }
}

上有此示例。我的问题是,如果Future投掷和异常的计算会在循环永远运行时?


我认为,它将永远循环。

首先,如果未来计算会引发异常,则

  • future.isdone()

始终评估true。因此,我们到达的内部,可以在其中设置停止条件。还可以。

第二,根据未来, ,当达到行

  • result = future.get();

时,它将始终抛出executionException,因为计算会引发异常。此例外是在try-catch块中捕获的,而无需达到停止条件先决条件,即

  • listan = false = false

最后,以上将创建一个无尽的循环。

我的假设中有什么错误,还是示例的作者真的错了?

I am reading the book Introducing Play Framework: Java Web Application Development (ISBN 978-1-4842-5645-9) and there is this example on Callable:

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableClient {
    /**
     * @param args
     */

    // Step1 : Create a Runnable
    public static void main(String[] args) {
        Callable callableTask = new CallableTask();
        // Step 2: Configure Executor
        // Uses FixedThreadPool executor
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<String> future = executor.submit(callableTask);
        boolean listen = true;
        while (listen) {
            if (future.isDone()) {
                String result;
                try {
                    result = future.get();
                    listen = false;
                    System.out.println(result);
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
        executor.shutdown();
    }
}

My question is, if the computation of the Future throws and exception, will the while loop run forever?


In my opinion, yes, it will loop forever.

First, if the Future computation throws an exception, then

  • future.isDone()

always evaluates to true. So we reach the inside of the if, where the stopping condition can be set. Still Ok.

Second, as per future documentation, when reaching line

  • result = future.get();

it will always throw ExecutionException because the computation threw an exception. This exception is caught in the try-catch block, without reaching the stopping condition precondition, which is

  • listen = false

Lastly, the above will create an endless cycle.

Is there any mistake in my assumptions or is the author of the example really wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2025-02-10 08:51:56

上面提到的程序循环永远,如果可可的例外。


这是一个带有可可的代码段,可引发异常。执行编译的片段循环。

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class CallableClient {

    public static class CallableTask implements Callable<String> {

        @Override
        public String call() throws Exception {
            throw new Exception();
        }
    }

    public static void main(String[] args) {
        Callable<String> callableTask = new CallableTask();
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<String> future = executor.submit(callableTask);
        boolean listen = true;
        while (listen) {
            if (future.isDone()) {
                String result;
                try {
                    result = future.get();
                    listen = false;
                    System.out.println(result);
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
        executor.shutdown();
    }
}

The program mentioned above cycles forever, if the callable throws an exception.


This is a code snippet with the callable that throws an exceptions. Executing compiled snippet loops forever.

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class CallableClient {

    public static class CallableTask implements Callable<String> {

        @Override
        public String call() throws Exception {
            throw new Exception();
        }
    }

    public static void main(String[] args) {
        Callable<String> callableTask = new CallableTask();
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future<String> future = executor.submit(callableTask);
        boolean listen = true;
        while (listen) {
            if (future.isDone()) {
                String result;
                try {
                    result = future.get();
                    listen = false;
                    System.out.println(result);
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }
        executor.shutdown();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文