笔者最近在看《java多线程编程核心技术》一书时,看到一个例子说明如何区分Thread.currentThread 和this ,特此分享。
例子如下:
package test;
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");
}
@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("run-----end");
}
}
package test;
public class Run {
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.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
CountOperate-----end
main begin t1 isAlive = false
main end t1 isAlive = true
run-----begin
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
run-----end
结果分析:
首先
isAlive()判断线程是否处于活动状态,活动状态就是指线程已经启动,尚未终止。
其次,Thread t1 = new Thread(c);调用的是如下构造函数(jdk源码如下)
/**
* Allocates a new {@code Thread} object. This constructor has the same
* effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
* {@code (null, target, gname)}, where {@code gname} is a newly generated
* name. Automatically generated names are of the form
* {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
*
* @param target
* the object whose {@code run} method is invoked when this thread
* is started. If {@code null}, this classes {@code run} method does
* nothing.
*/
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
本例中一共有三个线程
main 主线程
thread-0 : 也就是CoutOperate产生的线程
thread-1: 也就是“A”
提前说明一下:currentThread()方法返回的是代码段正在被哪个线程调用的信息
调用CountOperate产生结果:
CountOperate-----begin
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
CountOperate-----end
很好理解,main函数调用的CountOperate,所以Thread.currentThread()是main, 此时main肯定是活跃的(否则怎么调用别的线程呢),this肯定指的是当前类了,这个线程thread-0只是生成并没有被start或run所以isAlive是false;
main begin t1 isAlive = false
main end t1 isAlive = true
这个结果很好立即,请看Run()函数,这两个打印之间存在一个t1.start();代码,所以一个start之前是false,之后是true(也不一定,如果中间存在时间较长的话,t1就结束了,也可能是false)。
至于结果:
run-----begin
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
run-----end
是在t1进行start产生的,具体细节过程如下:
Thread对象进行start启动线程,我们直接启动的是Thread(Runnable target),所以c作为构造参数传给Thread中的属性target,之后,在Thread的run方法中,调用target.run(),此时Thread.currentThread()指的就是Thread的引用,也就是为什么这里Thread.currentThread()是A(thread-1)的原因了,而this依旧是myThread的引用,也就是说,this的这个线程还是thread-0,从这里可以看出二者区别了,值得注意的是笔者认为thread-0这个线程从头至尾一致没有执行过,就是说,这个的iaAlive一致是false,如果不对之处,请指正,感谢!