Java 主线程会在子线程完成执行之前退出吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9651842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:39:10  来源:igfitidea点击:

will main thread exit before child threads complete execution?

java

提问by user1257836

will main thread exit before child threads complete execution?

主线程会在子线程完成执行之前退出吗?

i read in 2 articles

我读了 2 篇文章

http://www.cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/thread-management.html

http://www.cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/thread-management.html

in the above article, In "Thread Termination" para, it states in Red " if the parent thread terminates, all of its child threads terminate as well."

在上面的文章中,在“线程终止”段落中,它以红色表示“如果父线程终止,其所有子线程也会终止。”

http://www.roseindia.net/java/thread/overview-of-thread.shtml

http://www.roseindia.net/java/thread/overview-of-thread.shtml

in the above article, the last line in that page states "The main() method execution can finish, but the program will keep running until the all threads have complete its execution.".

在上面的文章中,该页面的最后一行指出“main() 方法执行可以完成,但程序将继续运行,直到所有线程都执行完毕。”。

i fee they are contradictory. if i am wrong, Please experts correct me.

我觉得他们是矛盾的。如果我错了,请专家纠正我。

In my program, a program with Main method calls the constructor of 2 threads . in the constructor of the respective threads, i am having the start() method .

在我的程序中,一个带有 Main 方法的程序调用了 2 个线程的构造函数。在各个线程的构造函数中,我有 start() 方法。

     TestA  A = new TestA("TestA");
     TestB  B = new TestB("TestB");

     public TestA(String name) {
    System.out.println(name);
    t = new Thread(this);
    t.start();
}

i would like to know what happens, main thread terminates before child threads complete execution? if so, will the child threads anyway, continue their execution??

我想知道会发生什么,主线程在子线程完成执行之前终止?如果是这样,无论如何,子线程会继续执行吗?

i tried running the program, some times all the child threads are getting executed complete even if the main thread exits. In the 2 threads , i am processing some files. in testA thread A alone, 1 file alone is not getting processed some times. but many times, all the files are getting processed and i do not have any issues.

我尝试运行该程序,有时即使主线程退出,所有子线程也会执行完成。在 2 个线程中,我正在处理一些文件。在单独的 testA 线程 A 中,单独的 1 个文件有时没有得到处理。但很多时候,所有文件都得到处理,我没有任何问题。

采纳答案by Suresh Kumar

Java makes a distinction between a user thread and another type of thread known as a daemon thread. The difference between these two types of threads is that if the JVM determines that the only threads running in an application are daemon threads (i.e., there are no user threads), the Java runtime closes down the application. On the other hand, if at least one user thread is alive, the Java runtime won't terminate your application.

Java 区分了用户线程和另一种称为守护线程的线程。这两种线程的区别在于,如果 JVM 确定应用程序中运行的唯一线程是守护线程(即没有用户线程),则 Java 运行时会关闭应用程序。另一方面,如果至少有一个用户线程处于活动状态,则 Java 运行时不会终止您的应用程序。

When your main() method initially receives control from the Java runtime, it executes in the context of a user thread. As long as the main-method thread or any other user thread remains alive, your application will continue to execute.

当 main() 方法最初从 Java 运行时接收控制时,它会在用户线程的上下文中执行。只要主方法线程或任何其他用户线程保持活动状态,您的应用程序就会继续执行。

In your case, the threads are user threads and hence are allowed to complete before the main thread exits.

在您的情况下,线程是用户线程,因此允许在主线程退出之前完成。

i am processing some files. in testA thread A alone, 1 file alone is not getting processed some times. but many times

我正在处理一些文件。在单独的 testA 线程 A 中,单独的 1 个文件有时没有得到处理。但很多时候

The reason for the above is could be something else than thread exits. It could be file locks, synchronization issue etc.

上述原因可能是线程退出以外的其他原因。可能是文件锁定、同步问题等。

https://docs.oracle.com/javase/10/docs/api/java/lang/Thread.html:

https://docs.oracle.com/javase/10/docs/api/java/lang/Thread.html

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place. 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.

当 Java 虚拟机启动时,通常会有一个非守护线程(通常调用某个指定类的名为 main 的方法)。Java 虚拟机继续执行线程,直到发生以下任一情况:

已调用 Runtime 类的退出方法并且安全管理器已允许退出操作发生。不是守护线程的所有线程都已死亡,无论是从对 run 方法的调用返回,还是通过抛出传播到 run 方法之外的异常。

回答by kasavbere

Once the main thread exits, it takes the children with it. Perhaps by "finish" the second article simply means no more operation other than waiting for the children. Once the main thread calls System.exit(0); it's over -- every body dies.

一旦主线程退出,它就会带着孩子。也许“完成”第二条只是意味着除了等待孩子们之外没有更多的操作。一旦主线程调用 System.exit(0); 结束了——每个人都死了。

Say you have two threads running: threadA and threadB. in the main method. The first code is the nice way of terminating the thread -- just one of many ways:

假设您有两个线程在运行:threadA 和 threadB。在主方法中。第一个代码是终止线程的好方法——只是许多方法之一:

 threadA.start();
 threadB.start();
 final long intercept = 300;
 long startTime = System.currentTimeMillis();
 while (threadA.isAlive() && mis.isAlive()) {
    threadA.join(intercept);
if (System.currentTimeMillis() - startTime > intercept) {
   threadB.interrupt();
   threadA.interrupt();
   threadA.join();
}
}
System.exit(0);

Below is an abrupt way of killing all threads from within main:

下面是从 main 中杀死所有线程的突然方式:

System.exit(0);

回答by John

The background threads will keep running, even if the MAIN thread completes.

即使主线程完成,后台线程也会继续运行。

If you want MAIN to stop them (example, when MAIN is complete), have your MAIN set a "keep running" flag variable (which you must set as "volatile"), which the threads occasionally look at. MAIN sets it to false (variable) or null (object) when MAIN wants to stop them. When it's false or null, then the thread must "return;".

如果您希望 MAIN 停止它们(例如,当 MAIN 完成时),让您的 MAIN 设置一个“保持运行”标志变量(您必须将其设置为“易失性”),线程偶尔会查看该变量。当 MAIN 想要停止它们时,MAIN 将其设置为 false(变量)或 null(对象)。当它为 false 或 null 时,线程必须“返回;”。

This is somewhat complex to implement, there are many ways, but easiest is to make your Runnable an inner class, so that your Runnable has easy sharing of the flag.

这实现起来有点复杂,有很多方法,但最简单的方法是让你的 Runnable 成为一个内部类,这样你的 Runnable 就可以轻松共享标志。

For the best implementations, look up this technique in Java applets' start/stop routines.

要获得最佳实现,请在 Java 小程序的启动/停止例程中查找此技术。