Thread.currentThread()与this的区别【待完善】
package grammar.Thread.Chapter1;
public class CountOperate extends Thread {
public CountOperate(){
System.out.println("CountOperate - begin");
System.out.println("Thread.getCurrentThread().getName() = " + Thread.currentThread().getName());
System.out.println("Thread.getCurrentThread().isAlive() = " + Thread.currentThread().isAlive());
System.out.println("Thread.currentThread().getClass() = "+Thread.currentThread().getClass()+"\n\n");
System.out.println("this.getName() = " + this.getName());
System.out.println("this.isAlive() = " + this.isAlive());
System.out.println("this.getClass() is = "+this.getClass());
System.out.println("CountOperate - end\n\n");
}
@Override
public void run() {
System.out.println("run - begin");
System.out.println("Thread.getCurrentThread().getName() = " + Thread.currentThread().getName());
System.out.println("Thread.getCurrentThread().isAlive() = " + Thread.currentThread().isAlive());
System.out.println("Thread.currentThread().getClass() = "+Thread.currentThread().getClass()+"\n\n");
System.out.println("this.getName() = " + this.getName());
System.out.println("this.getClass() is = "+this.getClass());
System.out.println("this.isAlive() = " + this.isAlive());
System.out.println("run - end\n\n");
}
}
package grammar.Thread.Chapter1;
public class Test {
public static void main(String[] args) {
CountOperate c = new CountOperate();
Thread t1 = new Thread(c);
System.out.println("Main begin t1 isAlive = " + t1.isAlive());
t1.setName("A");
t1.start();
System.out.println("Main end t1 isAlive = "+ t1.isAlive());
}
}
执行结果如下:
CountOperate - begin
Thread.getCurrentThread().getName() = main
Thread.getCurrentThread().isAlive() = true
Thread.currentThread().getClass() = class java.lang.Thread
this.getName() = Thread-0
this.isAlive() = false
this.getClass() is = class grammar.Thread.Chapter1.CountOperate
CountOperate - end
Main begin t1 isAlive = false
Main end t1 isAlive = true
run - begin
Thread.getCurrentThread().getName() = A
Thread.getCurrentThread().isAlive() = true
Thread.currentThread().getClass() = class java.lang.Thread
this.getName() = Thread-0
this.getClass() is = class grammar.Thread.Chapter1.CountOperate
this.isAlive() = false
run - end