多线程--this和Thread.currentThread()详解

本文探讨了Java多线程中this和Thread.currentThread()的区别。通过实例分析,指出this指代的是对象实例,而Thread.currentThread()返回当前正在执行的线程。在构造函数中,this指向的是countoperate对象,而Thread.currentThread().getName()显示为子线程名称。深入源码,解析了线程启动时this的指向以及线程名的生成过程。
摘要由CSDN通过智能技术生成
在看多线程编程核心技术的时候,有一段代码让我很困惑,所以在这里记录一下。
public class isalive {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Thread.currentThread().getName();
		CountOperate countOperate=new CountOperate();
		Thread t1=new Thread(countOperate);
		System.out.println("main begin t1 isalive  "+t1.isAlive());
		t1.setName("a");
		t1.start();
		System.out.println("main end  "+t1.isAlive());
	}

}

class CountOperate extends Thread{
	public CountOperate(){
		System.out.println("构造函数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.Alive  "+this.isAlive());
		System.out.println("测试:"+Thre
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值