[英]How can I ensure all Java threads die
I am writing a multithreaded Java program, which I will run from the command prompt. 我正在编写一个多线程Java程序,该程序将在命令提示符下运行。 It's got both a gui (javafx) and a lot of background threads.
它同时具有gui(javafx)和许多后台线程。 If I hit Ctrl+C from the command prompt that I started the program with, it asks "Terminate Batch job? Y/N" and I hit Y and then I'm simply returned to the prompt.
如果我从启动程序的命令提示符处按Ctrl + C,它将询问“终止批处理作业?是/否”,然后单击Y,然后我将直接返回到提示符。 However - I am wondering if there by any chance might still be running any background threads that weren't terminated gracefully?
但是-我想知道是否有可能仍在运行未正常终止的任何后台线程?
Your "main" code and all the threads run in JVM
. 您的“主”代码和所有线程都在
JVM
运行。 If you terminate JVM (as process) then everything running in it will be terminated as well. 如果终止JVM(作为进程),则其中运行的所有内容也将终止。
More on "catching" ctrl+c
: Catching Ctrl+C in Java 有关“捕获”
ctrl+c
: Java中的Ctrl + C捕获
As far as I know, there's no way to "catch" or "intercept" Ctrl+C "event" from the command line, so your can't really tell if there are any unfinished threads and end them gracefully. 据我所知,无法从命令行“捕获”或“拦截” Ctrl + C“事件”,因此您无法真正分辨是否有未完成的线程并优雅地结束它们。 (disappointing, I know).
(令人失望,我知道)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.