您的位置:首页 > 编程语言 > Java开发

JAVA多线程编程之Thread中This和Thread.CurrentThread的区别

2017-04-25 15:53 495 查看
                                                                                      JAVA多线程编程之Thread中This和Thread.CurrentThread的区别

this代表当前的线程类,,[b]Thread.CurrentThread代表正在执行的线程,看例子就懂了:[/b]

1 自定义线程类


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 ");

        System.out.println("Thread.currentThread()==this :"+ (Thread.currentThread() == this));

    }

    @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("Thread.currentThread()==this :"+ (Thread.currentThread() == this));

        System.out.println("this.getName()=" + this.getName());

        System.out.println("this.isAlive()=" + this.isAlive());

        System.out.println("run --- end");

    }

}
2主函数

 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());

    }

}

3打印的Log为


CountOperate---begin

Thread.currentThread().getName()=main                 //主线程调用构造

Thread.currentThread().isAlive()=true                        //主线程存活

this.getName=Thread-0                                               //this代表这个线程对象CountOperate
的名字      没给定义系统默认定义Thread-0

this.isAlive()=false                                                         //这个自定义线程CountOperate
没启动  不是活动

CountOperate---end

Thread.currentThread()==this :false                         //当前运行的线程是主线程     this代表CountOperate   
肯定不相等

main begin t1 isAlive=false                                        //t!没启动   为不活动状态

main end t1 isAlive=true                                             //启动为活动状态

run---begin

Thread.currentThread().getName=A                         //t1.setName("A");     t1调用的run方法    给t1设置线程的名字

Thread.currentThread().isAlive()true                       

Thread.currentThread()==this :false                        //正在运行的是t1     this还是自定义那个线程类

this.getName()=Thread-0                                         

this.isAlive()=false

run --- end

4 这里比较让人疑惑的是“this.getName() = Thread-0”,这个Thread-0是什么东西??? 


通过查看Thread源码发现,在Thread类的构造方法中,会自动给name赋值,赋值代码:






说明此时的this和Thread.currentThread()指向不是同一个线程实例

也就是说,this指向的还是new CountOperate()创建的那个线程实例,而不是new Thread(thread)创建的那个实例即t1。


实际上new Thread(thread)会将thread应用的对象绑定到一个pravite变量target上,
在t1被执行的时候即t1.run()被调用的时候,它会调用target.run()方法,也就是说它是直接调用thread对象的run方法,
再确切的说,在run方法被执行的时候,this.getName()实际上返回的是target.getName(),而Thread.currentThread().getName()实际上是t1.getName()。

5修改代码

只启动自定义的线程


public class Run {

    public static void main(String[] args){

        CountOperate c = new CountOperate();

        c.start();

    }

}

更改后的打印结果


CountOperate---begin

Thread.currentThread().getName()=main

Thread.currentThread().isAlive()=true

this.getName=Thread-0

this.isAlive()=false

CountOperate---end

Thread.currentThread()==this :false

run---begin

Thread.currentThread().getName=Thread-0

Thread.currentThread().isAlive()true

Thread.currentThread()==this :true

this.getName()=Thread-0

this.isAlive()=true

run --- end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息