Thread.currentThread()与this的区别

在自定义线程类时,若是线程类是继承java.lang.Thread的话,那么线程类就可使用this关键字去调用继承自父类Thread的方法,this就是当前的对象。java

另外一方面,Thread.currentThread()能够获取当前线程的引用,通常都是在没有线程对象又须要得到线程信息时经过Thread.currentThread()获取当前代码段所在线程的引用。ide

尽管this与Thread.currentThread() 均可以获取到Thread的引用,可是在某种状况下获取到的引用是有差异的,下面进行举例说明函数

public class MyThread extends Thread {
	
	public MyThread(){
		
		System.out.println("------" + "构造函数开始" + "------");
		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("------" + "构造函数结束" + "------");
		
	}
	
	@Override
	public void run(){
		
		Thread testThread = Thread.currentThread();
		
		System.out.println("------" + "run()开始" + "------");
		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()结束" + "------");
		
	}
	
}

测试类:

public class Test {
	public static void main(String[] args){
		
		MyThread myThread = new MyThread();
		myThread.setName("A");
		myThread.start();
		
	}
}

测试结果:

------构造函数开始------
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
------构造函数结束------
------run()开始------
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = A
this.isAlive() = true
Thread.currentThread() == this : true
------run()结束------
测试


咱们会发现this 与 Thread.currentThread()是同一个引用this

先说构造方法中的代码结果

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
code

以后是run()中的代码结果
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true

当前线程名字为A,A是咱们手动赋予的myThread.setName("A");,而且它是运行着的

this.getName() = A
this.isAlive() = true

由于,咱们运行的线程就是MyThread的引用,而this也是MyThread的引用,因此打印结果与Thread.currentThread()相同,
而且Thread.currentThread() == this : true对象


当咱们保持线程类不变,以下修改测试类:blog

public class Test {
	public static void main(String[] args){
		
		MyThread myThread = new MyThread();
		// 将线程对象以构造参数的方式传递给Thread对象进行start()启动线程
		Thread newThread = new Thread(myThread);
		newThread.setName("A");
		newThread.start();
		
	}
}

测试结果以下:

------构造函数开始------
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
------构造函数结束------
------run()开始------
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
Thread.currentThread() == this : false
------run()结束------
继承

会发现,咱们会发现this 与 Thread.currentThread()不是同一个引用


将线程对象以构造参数的方式传递给Thread对象进行start()启动线程,咱们直接启动的线程实际是newThread,而做为构造参数的myThread,赋给Thread类中的属性target,以后在Thread的run方法中调用target.run(); 此时Thread.currentThread()是Thread的引用newThread, 而this依旧是MyThread的引用,因此是不同的,打印的内容也不同