<legend id='Bzbaf'><style id='Bzbaf'><dir id='Bzbaf'><q id='Bzbaf'></q></dir></style></legend>

  • <small id='Bzbaf'></small><noframes id='Bzbaf'>

        <i id='Bzbaf'><tr id='Bzbaf'><dt id='Bzbaf'><q id='Bzbaf'><span id='Bzbaf'><b id='Bzbaf'><form id='Bzbaf'><ins id='Bzbaf'></ins><ul id='Bzbaf'></ul><sub id='Bzbaf'></sub></form><legend id='Bzbaf'></legend><bdo id='Bzbaf'><pre id='Bzbaf'><center id='Bzbaf'></center></pre></bdo></b><th id='Bzbaf'></th></span></q></dt></tr></i><div id='Bzbaf'><tfoot id='Bzbaf'></tfoot><dl id='Bzbaf'><fieldset id='Bzbaf'></fieldset></dl></div>
          <bdo id='Bzbaf'></bdo><ul id='Bzbaf'></ul>
        <tfoot id='Bzbaf'></tfoot>
      1. 如果在 jUnit 测试中,为什么非守护线程会终止?

        时间:2023-08-24

            • <bdo id='ldudx'></bdo><ul id='ldudx'></ul>
                <tbody id='ldudx'></tbody>

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

                  <tfoot id='ldudx'></tfoot>

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

                  本文介绍了如果在 jUnit 测试中,为什么非守护线程会终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下 daemon-bean 正在运行:

                  The following daemon-bean is running:

                  public class DaemonBean extends Thread {
                  
                      private final static Logger log = LoggerFactory.getLogger(DaemonBean.class);
                  
                      {
                          setDaemon(true);
                          start();
                      }
                  
                      @Override
                      public void run() {
                  
                          for(int i=0; i<10 && !isInterrupted(); ++i) {
                              log.info("Hearbeat {}", i);
                              try {
                                  sleep(1000);
                              } catch (InterruptedException e) {
                                  return;
                              }
                          }
                  
                      }
                  }
                  

                  它是守护进程,所以如果单例会终止.

                  It is daemon, so would terminate if singleton.

                  所以,下面的非守护进程 bean 正在等待他:

                  So, the following non-daemon bean is waiting for him:

                  public class Waitor1 extends Thread {
                  
                      private final static Logger log = LoggerFactory.getLogger(Waitor1.class);
                  
                      private Thread joinable;
                  
                      {
                          setDaemon(false);
                          setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                  
                              @Override
                              public void uncaughtException(Thread t, Throwable e) {
                                  log.error("Error in thread", e);
                              }
                          });
                      }
                  
                      public Thread getJoinable() {
                          return joinable;
                      }
                  
                      public void setJoinable(Thread value) {
                          this.joinable = value;
                          if( this.joinable != null ) {
                              start();
                          }
                      }
                  
                      @Override
                      public void run() {
                  
                          log.info("Waiting started");
                  
                          try {
                              joinable.join();
                          } catch (InterruptedException e) {
                              log.info("Thread interrupted");
                              return;
                          }
                  
                          log.info("Waiting ended");
                  
                      }
                  
                  }
                  

                  bean 的 Spring 配置是:

                  The Spring configuration for beans is:

                  <bean id="daemon" class="beans.DaemonBean"/>
                  
                      <bean id="waitor" class="beans.Waitor1">
                          <property name="joinable" ref="daemon"/>
                      </bean>
                  

                  问题是:为什么从 main 运行它可以工作,而如果从 jUnit 测试运行它却不工作?

                  The question is: why is it working if runned from main and not working if ran from jUnit test?

                  运行代码是

                   public static void main(String[] args) {
                          new ClassPathXmlApplicationContext("/beans/Waiting1.xml");
                      }
                  

                  @Test
                      public void testWaiting1() {
                          new ClassPathXmlApplicationContext("/beans/Waiting1.xml");
                      }
                  

                  在 main 的情况下,我看到所有的心跳.在 jUnit 的情况下,我只看到心跳 0,然后消息等待开始"并且程序终止,就好像没有人在这里等待非守护线程一样.

                  In case of main I see all hearbeats. In case of jUnit I see only heartbeat 0, then message "Waiting started" and the program is terminated as if nobody waiting for non-daemon threads here.

                  这可能是什么原因?

                  推荐答案

                  当您从 main 运行代码时,它会创建两个 bean,因此会创建两个线程 - 守护进程和非守护进程.只要非守护线程正在运行,您的应用程序就不会退出.所以它起作用了.

                  When you run your code from main it creates both beans, thus two threads - daemon and non-daemon. As long as non-daemon thread is running, your application won't exit. So it works.

                  从 JUnit 运行时不同.一旦 JUnit 测试方法完成(并且它在 Spring 上下文启动后立即完成),JUnit 就会假定您的测试已经完成.因此它会杀死你所有的线程,基本上是整个 JVM.

                  It's different when run from JUnit. As soon as JUnit test method completes (and it completes immediately after the Spring context is up), JUnit assumes your tests are done. Thus it kills all your threads and basically the whole JVM.

                  记住你的 Waitor1 bean 会产生一个 JUnit 不关心的后台线程.一旦你离开 @Test 方法,JUnit 就会停止一切.

                  Remember your Waitor1 bean spawns a background thread which JUnit doesn't care about. As soon as you leave @Test method JUnit will just stop everything.

                  这篇关于如果在 jUnit 测试中,为什么非守护线程会终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在未捕获的异常上生成 Java 堆转储 下一篇:我从来没有见过一个软件以 jar 的形式发布,那么我怎样才能让一个软件也以 exe 的形式发布呢?

                  相关文章

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

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

                      <legend id='MDPw6'><style id='MDPw6'><dir id='MDPw6'><q id='MDPw6'></q></dir></style></legend>
                    1. <tfoot id='MDPw6'></tfoot>