简体   繁体   English

如何确保所有Java线程死亡

[英]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+cJava中的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.

相关问题 如果线程死亡,我该如何重新生成线程 - How do I respawn threads if they die Java中的线程,具有main()的堆栈可以在具有run()的堆栈之前消亡吗? - Threads in Java,can the stack with main() die/end before the stack with run()? 如何确保线程的进程完全完成? - How can I ensure that the processes of the threads are completely finished? 如何确保Java线程在不同的核心上运行 - How to ensure Java threads run on different cores 如何确保 Java 线程按特定顺序运行 - How to ensure Java threads run in a particular order 如何确保git存储库中的所有Java项目都具有最新或所需的maven依赖项版本? - How can i ensure all my java projects in a git repository have latest or desired version of maven dependency? 在Java中,如何确保捕获和记录的所有异常都在系统中提供了堆栈跟踪? - In Java, how can I ensure all exceptions that are caught and logged provide a stacktrace in my system? 确保所有线程更新 Java 映射中的单个条目 - Ensure all threads updating a single entry in a map in Java 如何确保在主线程停止之前停止所有线程? - How do I ensure that all Threads are stopped before main Thread is stopped? 为什么在这段代码中 Java 的 Thread.join() 不等待所有线程死亡? - Why doesn't Java's Thread.join() wait for all threads to die in this code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM