Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP
package mythread;

public class CountOperate extends Thread{

    public CountOperate(){
System.out.println("CountOperate---begin");
System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());//获取线程名
System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive()); //查看线程是否存活
System.out.println("this.getName=" + this.getName());
System.out.println("this.isAlive()=" + this.isAlive());
System.out.println("CountOperate---end ");
System.out.println("Thread.currentThread()==this :"+ (Thread.currentThread() == this));
} @Override
public void run() {
System.out.println("run---begin");
System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive()" + Thread.currentThread().isAlive());
System.out.println("Thread.currentThread()==this :"+ (Thread.currentThread() == this));
System.out.println("this.getName()=" + this.getName());
System.out.println("this.isAlive()=" + this.isAlive());
System.out.println("run --- end");
}
}
Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP
Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP
public class Run {

    public static void main(String[] args){

        CountOperate c = new CountOperate();
c.start();
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()); }
}
Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

打印的log为:

Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

CountOperate---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=Thread-0
this.isAlive()=false
CountOperate---end 
Thread.currentThread()==this :false
main begin t1 isAlive=false
main end t1 isAlive=true
run---begin
Thread.currentThread().getName=A
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false
this.getName()=Thread-0
this.isAlive()=false
run --- end

Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

根据打印的Log可以知道调用CountOperate构造函数的是main线程,因此打印出

Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
而此时还没有启动CountOperate子线程所以打印出
this.getName=Thread-0
this.isAlive()=false 此时this代表的是CountOperate对象实例,所以
Thread.currentThread()==this :false

这里比较让人疑惑的是“this.getName() = Thread-0”,这个Thread-0是什么东西??? 
通过查看Thread源码发现,在Thread类的构造方法中,会自动给name赋值,赋值代码:

Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

然后执行到:
Thread t1 = new Thread(c);
System.out.println("main begin t1 isAlive=" + t1.isAlive());
t1.setName("A");
t1.start(); Log打印:
Thread.currentThread().getName=A
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false
this.getName()=Thread-0
this.isAlive()=false
说明此时的this和Thread.currentThread()指向不是同一个线程实例 也就是说,this指向的还是new CountOperate()创建的那个线程实例,而不是new Thread(thread)创建的那个实例即t1。
查看源代码可以知道。

Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

实际上new Thread(thread)会将thread应用的对象绑定到一个pravite变量target上,
在t1被执行的时候即t1.run()被调用的时候,它会调用target.run()方法,也就是说它是直接调用thread对象的run方法,
再确切的说,在run方法被执行的时候,this.getName()实际上返回的是target.getName(),而Thread.currentThread().getName()实际上是t1.getName()。 因此我们修改下main中的代码为:
Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP
public class Run {

    public static void main(String[] args){

        CountOperate c = new CountOperate();
c.start();
}
}
Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

打印的log为:

Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP
CountOperate---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=Thread-0
this.isAlive()=false
CountOperate---end
Thread.currentThread()==this :false
run---begin
Thread.currentThread().getName=Thread-0
Thread.currentThread().isAlive()true
Thread.currentThread()==this :true
this.getName()=Thread-0
this.isAlive()=true
run --- end
Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术-LMLPHP

与我们预想的结果相同

转载自:http://www.cnblogs.com/huangyichun/p/6071625.html

05-02 04:38