父线程停止是否会导致子线程停止呢?
不会,具体可看测试程序:
public class MultiThread {
public static void main(String args[]) throws Exception {
boolean flag = true;
while (flag) {
Thread fatherThread = new FatherThread();
fatherThread.start();
System.out.println("========");
flag = false;
}
}// main
}// MultiThread
class FatherThread extends Thread{
FatherThread() {
}// 构造函数
public void run() {
System.out.println("father thread start ...");
childThread childThread = new childThread(this);
childThread.start();
System.out.println("father thread sleep 100000");
try {
//把这个值设置的足够大就看到在子线程停止以后是否还有线程存在
//不存在说明子线程在运行5秒以后是有将父线程停止的
Thread.sleep(100000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class childThread extends Thread {
private FatherThread thread;
childThread(FatherThread thread) {
this.thread = thread;
}// 构造函数
public void run() {
System.out.println("child thread start ...");
try {
Thread.sleep(5000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(thread.getState());
thread.stop();
System.out.println("father thread stop ...");
System.out.println("child thread continue ...");
System.out.println("child thread sleep 5000");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将状态效果打印到这就能很明显的看到效果
System.out.println(thread.getState());
System.out.println(thread.isAlive());
}
}