Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么线程在主方法执行之后运行?_Java_Multithreading - Fatal编程技术网

Java 为什么线程在主方法执行之后运行?

Java 为什么线程在主方法执行之后运行?,java,multithreading,Java,Multithreading,输出为: 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() { Sy

输出为:

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

当任何非守护进程线程运行时,Java程序将继续运行。在下面的链接中:“守护进程线程是一个线程,当程序完成但线程仍在运行时,它不会阻止JVM退出。守护进程线程的一个示例是垃圾收集。您可以使用setDaemon()方法更改线程守护进程属性。”


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

当任何非守护进程线程运行时,Java程序将继续运行。在下面的链接中:“守护进程线程是一个线程,当程序完成但线程仍在运行时,它不会阻止JVM退出。守护进程线程的一个示例是垃圾收集。您可以使用setDaemon()方法更改线程守护进程属性。”


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

您没有要求主线程在退出之前等待
t1
完成,因此两个线程之间根本没有同步(并且您无法保证它们两个何时执行
println

如果要使主线程等待另一个线程,则应使用
join()
函数,例如:

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

您没有要求主线程在退出之前等待
t1
完成,因此这两个线程之间根本没有同步(并且您无法保证这两个线程何时执行其
println

如果要使主线程等待另一个线程,则应使用
join()
函数,例如:

Main Prgm Started...
Main Prgm Exited...
Thread is running..
或使用倒计时闩锁:

t1.start();
t1.join();
System.out.println("Main Prgm Exited...");
公共类测试线程{

import java.util.concurrent.CountDownLatch;
}或使用倒计时闩锁:

t1.start();
t1.join();
System.out.println("Main Prgm Exited...");
公共类测试线程{

import java.util.concurrent.CountDownLatch;

}

如果您想知道程序何时完成,您需要一个关机挂钩。从main返回不会终止程序

public static void main(String[] args) {

    final CountDownLatch countDownLatch = new CountDownLatch(1);

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

        @Override
        public void run() {

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

    t1.start();

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Main Prgm Exited...");
}

如果你想知道一个程序何时完成,你需要一个关闭钩子。从main返回不会终止程序

public static void main(String[] args) {

    final CountDownLatch countDownLatch = new CountDownLatch(1);

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

        @Override
        public void run() {

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

    t1.start();

    try {
        countDownLatch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("Main Prgm Exited...");
}

因为它在一个单独的线程中运行?除非同步线程,否则它们将独立运行,没有特定的顺序。因为它是在单独的线程中运行的?除非同步线程,否则它们将独立运行,没有特定的顺序。