<tfoot id='c3fVv'></tfoot>

      <legend id='c3fVv'><style id='c3fVv'><dir id='c3fVv'><q id='c3fVv'></q></dir></style></legend>
    1. <i id='c3fVv'><tr id='c3fVv'><dt id='c3fVv'><q id='c3fVv'><span id='c3fVv'><b id='c3fVv'><form id='c3fVv'><ins id='c3fVv'></ins><ul id='c3fVv'></ul><sub id='c3fVv'></sub></form><legend id='c3fVv'></legend><bdo id='c3fVv'><pre id='c3fVv'><center id='c3fVv'></center></pre></bdo></b><th id='c3fVv'></th></span></q></dt></tr></i><div id='c3fVv'><tfoot id='c3fVv'></tfoot><dl id='c3fVv'><fieldset id='c3fVv'></fieldset></dl></div>
      • <bdo id='c3fVv'></bdo><ul id='c3fVv'></ul>

        <small id='c3fVv'></small><noframes id='c3fVv'>

        JVM 如何终止守护线程?或如何编写优雅终止的守护线程

        How does the JVM terminate daemon threads? or How to write daemon threads that terminate gracefully(JVM 如何终止守护线程?或如何编写优雅终止的守护线程)
          • <small id='OSPvu'></small><noframes id='OSPvu'>

            • <bdo id='OSPvu'></bdo><ul id='OSPvu'></ul>
            • <i id='OSPvu'><tr id='OSPvu'><dt id='OSPvu'><q id='OSPvu'><span id='OSPvu'><b id='OSPvu'><form id='OSPvu'><ins id='OSPvu'></ins><ul id='OSPvu'></ul><sub id='OSPvu'></sub></form><legend id='OSPvu'></legend><bdo id='OSPvu'><pre id='OSPvu'><center id='OSPvu'></center></pre></bdo></b><th id='OSPvu'></th></span></q></dt></tr></i><div id='OSPvu'><tfoot id='OSPvu'></tfoot><dl id='OSPvu'><fieldset id='OSPvu'></fieldset></dl></div>
              <tfoot id='OSPvu'></tfoot><legend id='OSPvu'><style id='OSPvu'><dir id='OSPvu'><q id='OSPvu'></q></dir></style></legend>

                  <tbody id='OSPvu'></tbody>
                  本文介绍了JVM 如何终止守护线程?或如何编写优雅终止的守护线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设场景:
                  我有一个守护线程负责一些 I/O,主线程完成并返回,JVM 决定终止我的守护线程.

                  Hypothetical scenario:
                  I have a daemon thread responsible for some I/O, the main thread finishes and returns, and the JVM decides to terminate my daemon thread.

                  它是如何做到的?打断?敲定?如何编码我的守护线程,以便它在终止时做出优雅的反应?

                  How does it do so? Interrupt? Finalize? How can I code my daemon thread so that it reacts gracefully when terminated?

                  推荐答案

                  我只是写了以下代码作为测试:

                  I just wrote the following code as a test:

                  public class DaemonThreadPlay {
                      public static void main(String [] args) {
                          Thread daemonThread = new Thread() {
                              public void run() {
                                  while (true) {
                                      try {
                                          System.out.println("Try block executed");
                                          Thread.sleep(1000l);
                                      } catch (Throwable t) {
                                          t.printStackTrace();
                                      }
                                  }
                              }
                  
                              @Override
                              public void finalize() {
                                  System.out.println("Finalize method called");
                              }
                          };
                          daemonThread.setDaemon(true);
                          daemonThread.start();
                  
                          try {
                              Thread.sleep(2500l);
                          } catch (Throwable t) {
                              //NO-OP
                          }
                      }
                  }    
                  

                  我在守护线程的 catch 块和 finalize 方法中设置了断点.即使执行了 try 块,也没有到达断点.显然这段代码存在同步/计时问题,但我认为我们可以安全地得出结论,守护线程不会在关闭时中断,也不一定会调用它们的 finalize() 方法.

                  I put breakpoints in the catch block of the daemon thread and in the finalize method. Neither breakpoint was reached even though the try block was executed. Obviously this code has synchronization/timing issues, but I think we can safely conclude that daemon threads are not interrupted on shutdown nor are their finalize() methods necessarily invoked.

                  您始终可以向 JVM 运行时添加关闭挂钩:

                  You can always add a shutdown hook to the JVM Runtime:

                  Thread shutdownHook = ... // construct thread that somehow
                                            // knows about all the daemon threads
                  Runtime.getRuntime().addShutdownHook(shutdownHook);
                  

                  您的关闭挂钩显然可以执行优雅"关闭所需的任何任务.

                  Your shutdown hook can obviously do whatever tasks are required for a "graceful" shutdown.

                  这篇关于JVM 如何终止守护线程?或如何编写优雅终止的守护线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  1. <i id='vZqRJ'><tr id='vZqRJ'><dt id='vZqRJ'><q id='vZqRJ'><span id='vZqRJ'><b id='vZqRJ'><form id='vZqRJ'><ins id='vZqRJ'></ins><ul id='vZqRJ'></ul><sub id='vZqRJ'></sub></form><legend id='vZqRJ'></legend><bdo id='vZqRJ'><pre id='vZqRJ'><center id='vZqRJ'></center></pre></bdo></b><th id='vZqRJ'></th></span></q></dt></tr></i><div id='vZqRJ'><tfoot id='vZqRJ'></tfoot><dl id='vZqRJ'><fieldset id='vZqRJ'></fieldset></dl></div>
                      <bdo id='vZqRJ'></bdo><ul id='vZqRJ'></ul>
                        <tbody id='vZqRJ'></tbody>
                    • <legend id='vZqRJ'><style id='vZqRJ'><dir id='vZqRJ'><q id='vZqRJ'></q></dir></style></legend>
                    • <small id='vZqRJ'></small><noframes id='vZqRJ'>

                        • <tfoot id='vZqRJ'></tfoot>