为什么线程在执行 main 方法后运行? [英] Why Thread is running after execution of main method?

查看:51
本文介绍了为什么线程在执行 main 方法后运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class TestThread {

    public static void main(String[] args) {

        System.out.println("Main Prgm Started...");
        Thread t1=new Thread(new Runnable() {

            @Override
            public void run() {

                System.out.println("Thread is running..");
            }
        });

        t1.start();

        System.out.println("Main Prgm Exited...");
    }
}

输出为:

Main Prgm Started...
Main Prgm Exited...
Thread is running..

推荐答案

Java 程序将继续运行,而任何非 daemon 线程正在运行.从下面的链接:守护线程是一个线程,它不会阻止 JVM 在程序完成但线程仍在运行时退出.守护线程的一个例子是垃圾收集.您可以使用 setDaemon()更改线程守护程序属性的方法."Java 中的守护线程是什么?

Java programs will continue to run while any non-daemon thread is running. From the link below: "A daemon thread is a thread, that does not prevent the JVM from exiting when the program finishes but the thread is still running. An example for a daemon thread is the garbage collection. You can use the setDaemon() method to change the Thread daemon properties." What is Daemon thread in Java?

默认情况下,所有创建的线程都是守护进程.您需要将其设置为非守护进程.

By default all created threads are daemon. You need to set it as non daemon.

这篇关于为什么线程在执行 main 方法后运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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