为什么在jUnit测试中非守护进程线程终止? [英] Why non-daemon thread is terminating if in jUnit test?

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

问题描述

以下守护进程正在运行:

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");
    }

在主要情况下,我会看到所有听音.在使用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测试中非守护进程线程终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆