代理名称:Thread的名称
实际名称:对象的名称
package com.sxt.io2;
public class InfoTest {
public static void main(String[] args) throws InterruptedException {
System.out.println(Thread.currentThread().isAlive());
Thread t=new Thread(new MyInfo("战斗机"),"公鸡");
t.start();
Thread.sleep(1000);
System.out.println(t.isAlive());
}
}
class MyInfo implements Runnable{
String name;
public MyInfo(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+name);
}
}
问题总结:
要想检测一个线程执行完毕是否还活着,必须将线程赋给一个变量,以便再次调用。否则每次都用Thread.currentThread().isAlive(),每次都是新的线程,都会活着