这是我的代码,问题是子线程 T1 并未终止子线程 T ,请看源码
public class ThreadExit {
public static void main(String[] args) {
T1 t1 = new T1();
Thread thread = new Thread(t1);
thread.start();
T t = new T();
t.start();
}
}
class T extends Thread {
private int count = 0;
private boolean loop = true;
@SuppressWarnings("all")
@Override
public void run() {
while(loop){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread T in progress..." + (++count));
}
}
public void setLoop(boolean loop) {
this.loop = loop;
}
}
class T1 implements Runnable {
@Override
public void run() {
T t = new T();
System.out.println("Thread T1 whill to sleep for 5s ...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread T1'll stop T, T are about to exit !");
t.setLoop(false);
}
}