This question already has an answer here:
这个问题在这里已有答案:
- Does a child thread in Java prevent the parent threads to terminate? 4 answers
- Java中的子线程是否阻止父线程终止? 4个答案
There is something I can't understand with threads in general. In my case, using Java and Android.
总的来说,我无法理解线程。就我而言,使用Java和Android。
Let's say I have a Thread named A
, which launches thread B
. If thread A
stops, then thread B
will continue to live. How is this possible? Who belongs to the Thread B
now? To the main thread?
假设我有一个名为A的线程,它启动线程B.如果线程A停止,则线程B将继续存在。这怎么可能?谁现在属于线程B?到主线程?
Thread class
线程类
public class ParentThread extends Thread {
public void run(){
Log.e("PARENT THREAD", "STARTS RUNNING");
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
Log.e("CHILD THREAD", "IS ALIVE");
try {
Thread.sleep(1000);
}
catch (InterruptedException exc) {
e.printStackTrace();
}
}
}
}).start();
Log.e("PARENT THREAD", "STOPS RUNNING");
}
}
Activity
活动
new ParentThread().start();
Logcat output
Logcat输出
01-07 13:45:16.726 22063-22081/? E/PARENT THREAD: STARTS RUNNING
01-07 13:45:16.726 22063-22081/? E/PARENT THREAD: STOPS RUNNING
01-07 13:45:16.727 22063-22082/? E/CHILD THREAD: IS ALIVE
01-07 13:45:17.727 22063-22082/? E/CHILD THREAD: IS ALIVE
01-07 13:45:18.727 22063-22082/? E/CHILD THREAD: IS ALIVE
01-07 13:45:19.727 22063-22082/? E/CHILD THREAD: IS ALIVE
...
2 个解决方案
#1
3
From docs Thread
来自docs Thread
A Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Each application has at least one thread running when it is started, the main thread, in the main ThreadGroup. The runtime keeps its own threads in the system thread group.
线程是并发执行单元。它有自己的调用堆栈,用于调用方法,它们的参数和局部变量。每个应用程序在启动时至少有一个线程运行,主线程在主ThreadGroup中运行。运行时在系统线程组中保留自己的线程。
#2
2
Let's say I have a Thread named A, that launches a Thread B. If the Thread A stops, the Thread B will continue to live. How is this possible?
假设我有一个名为A的线程,它启动一个线程B.如果线程A停止,线程B将继续存在。这怎么可能?
It's easily possible - there's nothing that makes thread B stop, so it doesn't stop.
这很容易 - 没有什么能让线程B停止,所以它不会停止。
Who belongs to the Thread B now? To the main thread?
谁现在属于线程B?到主线程?
Threads don't belong to each other. The fact that it was Thread A that launched Thread B isn't recorded anywhere - as far as the system is concerned, you have two threads with no relationship between them whatsoever.
线程不属于彼此。启动线程B的是线程A这一事实并没有记录在任何地方 - 就系统而言,你有两个线程,它们之间没有任何关系。
#1
3
From docs Thread
来自docs Thread
A Thread is a concurrent unit of execution. It has its own call stack for methods being invoked, their arguments and local variables. Each application has at least one thread running when it is started, the main thread, in the main ThreadGroup. The runtime keeps its own threads in the system thread group.
线程是并发执行单元。它有自己的调用堆栈,用于调用方法,它们的参数和局部变量。每个应用程序在启动时至少有一个线程运行,主线程在主ThreadGroup中运行。运行时在系统线程组中保留自己的线程。
#2
2
Let's say I have a Thread named A, that launches a Thread B. If the Thread A stops, the Thread B will continue to live. How is this possible?
假设我有一个名为A的线程,它启动一个线程B.如果线程A停止,线程B将继续存在。这怎么可能?
It's easily possible - there's nothing that makes thread B stop, so it doesn't stop.
这很容易 - 没有什么能让线程B停止,所以它不会停止。
Who belongs to the Thread B now? To the main thread?
谁现在属于线程B?到主线程?
Threads don't belong to each other. The fact that it was Thread A that launched Thread B isn't recorded anywhere - as far as the system is concerned, you have two threads with no relationship between them whatsoever.
线程不属于彼此。启动线程B的是线程A这一事实并没有记录在任何地方 - 就系统而言,你有两个线程,它们之间没有任何关系。