在子线程完成执行之前主线程会退出吗? [英] will main thread exit before child threads complete execution?

查看:185
本文介绍了在子线程完成执行之前主线程会退出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

will main thread exit before child threads complete execution?

我读了2篇文章

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

在上面的文章中,在线程终止中,它在Red中声明如果父线程终止,它的所有子线程也会终止。

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

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

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.".

我付费他们是矛盾的。如果我错了,请专家指正。

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

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

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 .

     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??

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

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.

推荐答案

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

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.

当main()方法最初从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.

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

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


我正在处理一些文件。在testA线程A中,仅1个文件是
有时没有被处理。但很多次

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

上述原因可能不是线程退出。它可能是文件锁,同步问题等。

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


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

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:

已调用类Runtime的exit方法并且安全性为
经理允许退出操作。所有不是守护程序线程的线程
都已经死亡,要么通过从
调用返回到run方法,要么抛出一个超出run方法传播
的异常。

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.

这篇关于在子线程完成执行之前主线程会退出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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