public class hello extends Thread {
public hello(String name){
super(name);
System.out.println("Thread.currentThread().getname()="+Thread.currentThread().getName());
System.out.println("This.getName="+this.getName());
}
public void run(){
System.out.println("Thread.currentThread().getname()="+Thread.currentThread().getName());
System.out.println("This.getName="+this.getName());
}
public static void main(String[] args){
Thread t1=new Thread(new hello("A"));
t1.setName("B");
t1.start();
}
}
得到Thread.currentThread().getname()=main
This.getName=A
Thread.currentThread().getname()=B
This.getName=A
为什么会得到B和A不一样的名字
this得到的是当前的对象。java为这个对象创建一个线程运行,这个线程也有对象,两个不是一回事。
this是线程对象本身,而后者是当前正在运行的线程对象。但是不一定是同一个线程对象,因为this这个线程不一定正是当前正在运行的线程。
this是类进程,Thread.currentThread()是线程进程。