Java 中的子线程是否阻止父线程终止?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19988092/
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
Does a child thread in Java prevent the parent threads to terminate?
提问by Johnny
When I create and start a thread inside another thread, will it be a child thread? Does it prevent the termination of the parent thread while the child thread is running? For example:
当我在另一个线程中创建并启动一个线程时,它会是一个子线程吗?它是否会在子线程运行时阻止父线程的终止?例如:
new Thread(new Runnable() {
@Override
public void run() {
//Do Sth
new Thread(new Runnable() {
@Override
public void run() {
//Do Sth
new Thread(new Runnable() {
@Override
public void run() {
//Do Sth
}
}).start();
//Do Sth
}
}).start();
//Do Sth
}
//Do Sth
}).start();
采纳答案by NPE
In your code, there is a parent-child relationship between objects. However, there is no notion of a parent-child relationship between threads. Once the two threads are running they're basically peers.
在您的代码中,objects之间存在父子关系。但是,线程之间没有父子关系的概念。一旦这两个线程运行,它们基本上是对等的。
Let's say that thread A starts thread B. Unless there's explicit synchronisation between them, either thread can terminate whenever it pleases without affecting the other thread.
假设线程 A 启动线程 B。除非它们之间存在显式同步,否则任何一个线程都可以随时终止而不会影响另一个线程。
Note that either thread can keep the processalive for as long as the thread lives. See What is Daemon thread in Java?
请注意,只要线程存在,任何一个线程都可以使进程保持活动状态。请参阅Java 中的守护进程线程是什么?
回答by Changgeng
It will become a child thread, but it won't prevent parent thread from termination.
它将成为子线程,但不会阻止父线程终止。
回答by Gray
When I create and start a thread inside another thread, will it be a child thread?
当我在另一个线程中创建并启动一个线程时,它会是一个子线程吗?
Java has no real concept of "child" threads. When you start a thread it inherits the daemon and priority from the "parent" but that's the end of the parent/child relationship.
Java 没有真正的“子”线程概念。当您启动一个线程时,它会从“父”继承守护进程和优先级,但这就是父/子关系的结束。
// in Thread.init()
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
When the thread starts it runs along side its "parent" and there is no linkage between the two.
当线程开始时,它沿着它的“父”运行,两者之间没有链接。
Does it prevent the termination of the parent thread while the child thread is running?
它是否会在子线程运行时阻止父线程的终止?
No it does not. However I suspect that you are really wondering if a thread can prevent the termination of the application. If the child thread is non-daemon then even if the main thread (which is also non-daemon) finishes, your application will wait for the child thread to finish. By definition, the JVM will wait for all non-daemon threads to finish. Once the last non-daemon thread finishes, the JVM can terminate.
不,不是的。但是我怀疑您真的想知道线程是否可以阻止应用程序的终止。如果子线程是非守护进程,那么即使主线程(也是非守护进程)完成,您的应用程序也会等待子线程完成。根据定义,JVM 将等待所有非守护线程完成。一旦最后一个非守护线程完成,JVM 就可以终止。
回答by Rakesh Kr
There is no parent-child relationship in two threads. After Completion of ParentThread
, ParentThread
does not waiting for a
ChildThread
.
两个线程之间没有父子关系。完成后ParentThread
,ParentThread
不等了
ChildThread
。
public class MainThread {
public static void main(String[] args) {
ParentThread pt=new ParentThread();
pt.start();
for(int i=1;i<=20;i++){
System.out.println("ParentThread alive: "+pt.isAlive());
try{
Thread.currentThread().sleep(500);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
class ParentThread extends Thread{
public void run(){
ChildThread ct=new ChildThread();
ct.start();
for(int i=1;i<=10;i++){
try{
System.out.println("ChildThread alive: "+ ct.isAlive() + ", i = "+i);
sleep(500);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
class ChildThread extends Thread{
public void run(){
for(int i=1;i<=20;i++){
try{
System.out.println("ChildThread i = "+i);
sleep(500);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
}
output (like):
输出(如):
ParentThread alive: true
ChildThread alive: true, i = 1
ChildThread i = 1
ParentThread alive: true
ChildThread i = 2
ChildThread alive: true, i = 2
ParentThread alive: true
ChildThread i = 3
ChildThread alive: true, i = 3
ParentThread alive: true
ChildThread i = 4
ChildThread alive: true, i = 4
ParentThread alive: true
ChildThread i = 5
ChildThread alive: true, i = 5
ParentThread alive: true
ChildThread i = 6
ChildThread alive: true, i = 6
ParentThread alive: true
ChildThread alive: true, i = 7
ChildThread i = 7
ParentThread alive: true
ChildThread i = 8
ChildThread alive: true, i = 8
ParentThread alive: true
ChildThread alive: true, i = 9
ChildThread i = 9
ParentThread alive: true
ChildThread alive: true, i = 10
ChildThread i = 10
ParentThread alive: true
ChildThread i = 11
ParentThread alive: false
ChildThread i = 12
ChildThread i = 13
ParentThread alive: false
ParentThread alive: false
ChildThread i = 14
ParentThread alive: false
ChildThread i = 15
ParentThread alive: false
ChildThread i = 16
ParentThread alive: false
ChildThread i = 17
ChildThread i = 18
ParentThread alive: false
ParentThread alive: false
ChildThread i = 19
ParentThread alive: false
ChildThread i = 20