解决方案
以下示例演示了如何通过扩展 Threda 类并使用 currentThread() 方法来检查线程是否处于活动状态。
public class TwoThreadMoove extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
printMsg();
}
}
public void printMsg() {
Thread t = Thread.currentThread();
String name = t.getName();
System.out.println("name = " + name);
}
public static void main(String[] args) {
TwoThreadMoove tt = new TwoThreadMoove();
tt.setName("Thread");
System.out.println("before start(), tt.isMoove() = " + tt.isMoove());
tt.start();
System.out.println("just after start(), tt.isMoove() = " + tt.isMoove());
for (int i = 0; i < 10; i++) {
tt.printMsg();
}
System.out.println("The end of main(), tt.isMoove()=" + tt.isMoove());
}
}