主线程可以在子线程之前死掉 [英] can the main thread die before the child thread

查看:167
本文介绍了主线程可以在子线程之前死掉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信主线程在子线程之前不能死掉。但有什么方法可以检查吗?我在下面写了一个简单的程序。任何人都可以证明它实际上将理论放在一边吗?

I believe that the main thread cannot die before the child thread. But is there any way to check that ? I wrote a simple program below. Can anyone prove it practically leaving theory aside ?

class childre extends Thread
{   
    public void run()
    {   
        for( int i=0 ; i<10 ;i++)
        {
            System.out.println( " child " + i);

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    }
}

public class ChildThreadb4main
{

/**
 * @param args
 */
    public static void main(String[] args)
    {
    // TODO Auto-generated method stub

        System.out.println("main");

        childre c1 = new childre();

        c1.start();
        for(int i=0;i<5;i++)
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println( " child thread alive ? " + c1.isAlive());
    }
}






根据詹姆斯的建议。我尝试了以下程序。


After suggestion from James. I tried the following program.

public class MainChildDie {

    public static void main(String ar[]){

        final Thread mainThread = Thread.currentThread();
        System.out.println("main run ");

        new Thread(){           

            public void run(){

                Thread childThread= Thread.currentThread();
                for(int i=0; i<10;i++){
                    System.out.println( "child"+i);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("main alive  " + mainThread.isAlive());
            }
        }.start();      
    }
}


推荐答案

来自 http://docs.oracle.com/javase/6 /docs/api/java/lang/Thread.html


Java虚拟机继续执行线程,直到
发生以下情况:

The Java Virtual Machine continues to execute threads until either of the following occurs:


  1. 已调用类Runtime的退出方法并且安全
    经理允许退出操作发生。

  1. The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.

所有不是守护程序线程的线程
都已经死亡,无论是通过从
调用返回到run方法还是抛出一个传播
超出run方法的异常。

All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.


在你的情况下,当主线程死亡时,JVM不会退出,因为你仍然有运行的创建线程,并且默认情况下它们是守护进程,因为:

In your case, when the main thread dies, the JVM does not exit, because you still have the created threads running, and they're daemon by default, because of this:


当且仅当创建它的线程当前标记为守护程序线程时,新创建的线程最初被标记为守护程序线程。 setDaemon方法可用于更改线程是否为守护进程。

The newly created thread is initially marked as being a daemon thread if and only if the thread creating it is currently marked as a daemon thread. The method setDaemon may be used to change whether or not a thread is a daemon.

引用: http://docs.oracle.com/javase/6/docs/ api / java / lang / Thread.html #setDaemon(boolean)

这篇关于主线程可以在子线程之前死掉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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