JAVA 多线程 this.getName()与Thread.currentThread().getName()
hansonzhe
回复
hansonzhe: [code=java]
public static void main(String a[]) throws InterruptedException {
Student S0 = new CurrentThreadTest(). new Student("Jack");
Thread T0 = new Thread(S0);
System.out.println("S0.getName " + S0.getName());
System.out.println("T0.getName " + T0.getName());
Thread.sleep(1000);
// currentThread() 是 T0,this.getName() 是 S0
System.out.println("T0.start();");
T0.start();
Thread.sleep(1000);
// currentThread() 是 S0,this.getName() 是 S0
System.out.println("S0.start();");
S0.start();
Thread.sleep(1000);
// 不会执行
System.out.println("T0.run();");
T0.run();
Thread.sleep(1000);
// currentThread() 是 main,this.getName() 是 S0
System.out.println("S0.run();");
S0.run();
Thread.sleep(1000);
}
}
[/code]
执行这段代码,会很清晰看出 Thread.currentThread().getName() 和 this.getName() 的区别。
|