参考资料:http://blog.csdn.net/yezis/article/details/57513130
在自定义线程类时,如果线程类是继承java.lang.Thread的话,那么线程类就可以使用this关键字去调用继承自父类Thread的方法,this就是当前的对象。
另一方面,Thread.currentThread()可以获取当前线程的引用,一般都是在没有线程对象又需要获得线程信息时通过Thread.currentThread()获取当前代码段所在线程的引用。
尽管this与Thread.currentThread() 都可以获取到Thread的引用,但是在某种情况下获取到的引用是有差别的。
线程类:
package com.ice.thread;
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());//isAlive()判断当前线程是否处于活动状态
System.out.println("this.getName()="+this.getName());//this实际上是指CountOperate类的引用
System.out.println("this.isAlive()="+this.isAlive());
System.out.println("Thread.currentThread() == this : " + (Thread.currentThread() == this));
System.out.println("CountOperate--end");
}
@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("this.getName()="+this.getName());
System.out.println("this.isAlive()="+this.isAlive());
System.out.println("Thread.currentThread() == this : " + (Thread.currentThread() == this));
System.out.println("run--end");
}
}
测试类:
package com.ice.thread;
public class Run {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
System.out.println("main begin countOperate isAlive()="+countOperate.isAlive());//countOperate是否处于活动状态
countOperate.setName("countOperate");//给countOperate线程类名称赋值
countOperate.start();
System.out.println("main end countOperate isAlive()="+countOperate.isAlive());
}
}
测试结果:
CountOperate--begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive()=false
Thread.currentThread() == this : false
CountOperate--end
main begin countOperate isAlive()=false
main end countOperate isAlive()=true
run--begin
Thread.currentThread().getName()=countOperate
Thread.currentThread().isAlive()=true
this.getName()=countOperate
this.isAlive()=true
Thread.currentThread() == this : true
run--end
分析:
我们会发现this 与 Thread.currentThread()是同一个引用。
先说构造方法中的代码结果:
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
这个结果是指实例化MyThread,调用MyThread构造方法是主线程main。
this.getName()=Thread-0
this.isAlive()=false
现在,这个this是什么?MyThread的引用,是个线程类,但是这个线程类并没有设置名字,所以Thread默认给了一个Thread-0。
默认名字的规则定义如下:
因为仅仅是运行构造方法,还未运行线程,所以this.isAlive() = false。
之后是run()中的代码结果:
Thread.currentThread().getName()=countOperate
Thread.currentThread().isAlive()=true
当前线程名字为countOperate,countOperate是我们手动赋予的myThread.setName(“countOperate”);,并且它是运行着的。
this.getName()=countOperate
this.isAlive()=true
因为,我们运行的线程就是MyThread的引用,而this也是MyThread的引用,所以打印结果与Thread.currentThread()相同,
并且Thread.currentThread() == this : true
当我们保持线程类不变,如下修改测试类:
package com.ice.thread;
public class Run {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
Thread t1 = new Thread(countOperate);// 将线程对象以构造参数的方式传递给Thread对象进行start()启动线程
System.out.println("main begin t1 isAlive()="+t1.isAlive());
t1.setName("t1");
t1.start();
System.out.println("main end t1 isAlive()="+t1.isAlive());
}
}
测试结果如下:
CountOperate--begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive()=false
Thread.currentThread() == this : false
CountOperate--end
main begin t1 isAlive()=false
main end t1 isAlive()=true
run--begin
Thread.currentThread().getName()=t1
Thread.currentThread().isAlive()=true
this.getName()=Thread-0
this.isAlive()=false
Thread.currentThread() == this : false
run--end
会发现,我们会发现this 与 Thread.currentThread()不是同一个引用.
将线程对象以构造参数的方式传递给Thread对象进行start()启动线程,我们直接启动的线程实际是t1,而作为构造参数的countOperate,赋给Thread类中的属性target,之后在Thread的run方法中调用target.run();
此时Thread.currentThread()是Thread的引用t1, 而this依旧是countOperate的引用,所以是不一样的,打印的内容也不一样.
以上大多来自于引用的博客,我自己的理解是:
在哪个类中使用this,那这个this就代表哪个类;Thread.currentThread()永远代表当前它所处的线程。