admin管理员组

文章数量:1612065

结论:

  • Thread.currentThread():获取当前线程的引用,既代码段正在被哪一个线程调用。
  • this:指当前对象
  • this.currentThread():与Thread.currentThread()一致,都是调用

实验代码:

public class CountOperate extends Thread {
	
    public CountOperate() {
		System.out.println("=========CountOperate start============");
		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("this.currentThread().getName() = " + this.currentThread().getName());
		System.out.println("this.currentThread().isAlive() = " + this.currentThread().isAlive());
		System.out.println("=========CountOperate end  ============");
	}

	@Override
	public void run() {
		System.out.println("=========run start============");
		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("this.currentThread().getName() = " + this.currentThread().getName());
		System.out.println("this.currentThread().isAlive() = " + this.currentThread().isAlive());
		System.out.println("=========run end  ============");
	}	
}

实例一:

public class TestCountOperate {
	public static void main(String[] args) {
		CountOperate countOperate = new CountOperate();
		countOperate.start();
	}
}

结果:

=========CountOperate start============
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
this.currentThread().getName() = main
this.currentThread().isAlive() = true
=========CountOperate end  ============
=========run start============
Thread.currentThread().getName() = Thread-0
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = true
Thread.currentThread() == this true
this.currentThread().getName() = Thread-0
this.currentThread().isAlive() = true
=========run end  ============

实例二:

public class TestCountOperate {
	public static void main(String[] args) {
		CountOperate countOperate = new CountOperate();
		Thread thread = new Thread(countOperate);
		thread.setName("A");
		thread.start();
	}
}

结论:

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

      将线程对象countOperate 以构造参数的方式传递给Thread对象进行start()启动线程,我们直接启动的线程实际是thread,而作为构造参数的countOperate,赋给Thread类中的属性target,之后在Thread的run方法中调用target.run();此时Thread.currentThread()是Thread的引用thread, 而this依旧是CountOperate的引用,所以是不一样的,打印的内容也不一样。

本文标签: 多线程区别threadcurrentThread