没有用于守护程序的守护进程线程的执行上下文

没有用于守护程序的守护进程线程的执行上下文

本文介绍了没有用于守护程序的守护进程线程的执行上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用我编写的通过Scala 2.10 Futures + Promises框架生成线程的各种新应用程序后立即退出JVM遇到了麻烦.

I am having trouble with the JVM immediately exiting using various new applications I wrote which spawn threads through the Scala 2.10 Futures + Promises framework.

似乎,至少在默认执行上下文中,即使我正在使用阻塞,例如

It seems that at least with the default execution context, even if I'm using blocking, e.g.

future { blocking { /* work */ }}

没有启动非守护程序线程,因此JVM认为它可以立即退出.

no non-daemon thread is launched, and therefore the JVM thinks it can immediately quit.

一个愚蠢的解决方法是启动一个虚拟的Thread实例,该实例只是在等待,但是随后我还需要确保该线程在进程完成后停止.

A stupid work around is to launch a dummy Thread instance which is just waiting, but then I also need to make sure that this thread stops when the processes are done.

那么我如何强制它们在非守护进程线程上运行?

So how to I enforce them to run on non-daemon threads?

推荐答案

在查看附加到ExecutionContext.global的默认ExecutionContext时,它属于派生联接类型,并且它使用的Threadfactory将线程设置为.如果要解决此问题,可以使用其他ExecutionContext,您可以自己设置.如果您仍然想要FJP品种(并且您可能会按照最佳比例进行操作),则应该可以通过链接并创建类似内容.或者只是通过Executors.newCachedThreadPool使用缓存的线程池,因为这不会在您的期货完成之前立即关闭.

In looking at the default ExecutionContext attached to ExecutionContext.global, it's of the fork join variety and the Threadfactory it uses sets the threads to daemon. If you want to work around this, you could use a different ExecutionContext, one you set up yourself. If you still want the FJP variety (and you probably do as it scales the best), you should be able to look at what they are doing in ExecutionContextImpl via this link and create something similar. Or just use a cached thread pool via Executors.newCachedThreadPool as that won't shut down immediately before your futures complete.

这篇关于没有用于守护程序的守护进程线程的执行上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:57