Java线程完成后悬挂在完成后()

发布于 2025-02-08 08:41:16 字数 845 浏览 2 评论 0原文

我有简单的测试代码:

public static void main(String[] args) {
    CompletionService<Integer> cs = new ExecutorCompletionService<>(Executors.newCachedThreadPool());
    cs.submit(new Callable<Integer>() {
        public Integer call(){
            try{
                Thread.sleep(3000); // Just sleep and print
                System.out.println("Sleeping thread: " + Thread.currentThread().getId());
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            return 10;
        }
    });
    try{
        Future<Integer> fi = cs.take();
        System.out.println(fi.get());
    }catch(Exception e){
        e.printStackTrace();
    }
}

我运行它,睡3秒钟,然后打印,

Sleeping thread: 14
10

但随后挂在那里,程序没有结束。

发生了什么,如何完成?

I've got simple test code:

public static void main(String[] args) {
    CompletionService<Integer> cs = new ExecutorCompletionService<>(Executors.newCachedThreadPool());
    cs.submit(new Callable<Integer>() {
        public Integer call(){
            try{
                Thread.sleep(3000); // Just sleep and print
                System.out.println("Sleeping thread: " + Thread.currentThread().getId());
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            return 10;
        }
    });
    try{
        Future<Integer> fi = cs.take();
        System.out.println(fi.get());
    }catch(Exception e){
        e.printStackTrace();
    }
}

I run it, sleep 3 seconds, and prints

Sleeping thread: 14
10

But then it hangs there, the program doesn't end.

What's happening, how to make it finish?

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

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

发布评论

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

评论(1

情绪失控 2025-02-15 08:41:16

如评论中提到的, tgdavies ,您的程序将在+/- 60秒后退出,因为那是默认超时的超时时间executorService opecutors.newcachedthreadpool()

如果您不想等60秒,则在提交任务后应关闭执行人服务。

例如:

public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        CompletionService<Integer> cs = new ExecutorCompletionService<>(executorService);
        cs.submit(new Callable<Integer>() {
            public Integer call() {
                try {
                    Thread.sleep(3000); // Just sleep and print
                    System.out.println("Sleeping thread: " + Thread.currentThread().getId());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return 10;
            }
        });
        try {
            Future<Integer> fi = cs.take();
            System.out.println(fi.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        executorService.shutdown();
    }
}

或者,使用自定义threadfactory来配置执行器服务以创建守护程序线程。只有在没有正常(非daemon)线程的情况下,执行实际工作的执行人服务不是一个问题,才能做到这一点。

As mentioned in the comments by tgdavies, your program will exit after +/- 60 seconds, because that is the default timeout for a thread without tasks in an ExecutorService created by Executors.newCachedThreadPool().

If you don't want to wait for 60 seconds, you should shutdown the executor service after you're done submitting tasks.

For example:

public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();
    try {
        CompletionService<Integer> cs = new ExecutorCompletionService<>(executorService);
        cs.submit(new Callable<Integer>() {
            public Integer call() {
                try {
                    Thread.sleep(3000); // Just sleep and print
                    System.out.println("Sleeping thread: " + Thread.currentThread().getId());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return 10;
            }
        });
        try {
            Future<Integer> fi = cs.take();
            System.out.println(fi.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    } finally {
        executorService.shutdown();
    }
}

Alternatively, configure the executor service to create daemon threads using a custom ThreadFactory. Only do this if it is not a problem that an executor service that is doing actual work gets "killed" when there are no more normal (non-daemon) threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文