多线程中this、Thread.currentThread() 和 this.currentThread()的区别

结论:

  • 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的引用,所以是不一样的,打印的内容也不一样。

回答: 在JavaThread.currentThread()是Thread类的一个静态方法,用于获取当前线程对象的一个引用。该方法返回的是当前正在执行的线程。在多线程编程,可以使用Thread.currentThread()来获取当前线程的一些信息,比如线程的名称、优先级等。使用Thread.currentThread().getName()可以获取当前线程的名称。在给定的引用代码,main方法使用了Thread.currentThread().getName()来获取主线程的名称。此外,currentThread类继承了Thread类并重写了run()方法,可以通过Thread.currentThread().getName()在run()方法获取当前线程的名称。因此,Thread.currentThread()方法在Java是用来获取当前线程对象的一个引用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Thread.currentThread().getName()](https://blog.csdn.net/w15558056319/article/details/118208957)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [Java Thread.currentThread()方法具有什么功能呢?](https://blog.csdn.net/qq_25073223/article/details/126221543)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值