<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 测试中,为什么非守护线程会终止?

        Why non-daemon thread is terminating if in jUnit test?(如果在 jUnit 测试中,为什么非守护线程会终止?)

            • <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 测试中,为什么非守护线程会终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                  • <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>

                            <tbody id='MDPw6'></tbody>

                          <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>