zoukankan      html  css  js  c++  java
  • Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术


    package
    mythread; 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"); } }
     
    public class Run {
    
        public static void main(String[] args){
    
            CountOperate c = new CountOperate();
            c.start();
            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());
    
        }
    }

    打印的log为:

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

    根据打印的Log可以知道调用CountOperate构造函数的是main线程,因此打印出

    Thread.currentThread().getName()=main
    Thread.currentThread().isAlive()=true
    而此时还没有启动CountOperate子线程所以打印出
    this.getName=Thread-0
    this.isAlive()=false

    此时this代表的是CountOperate对象实例,所以
    Thread.currentThread()==this :false

    这里比较让人疑惑的是“this.getName() = Thread-0”,这个Thread-0是什么东西??? 
    通过查看Thread源码发现,在Thread类的构造方法中,会自动给name赋值,赋值代码:

    然后执行到:
    Thread t1 = new Thread(c);
    System.out.println("main begin t1 isAlive=" + t1.isAlive());
    t1.setName("A");
    t1.start();

    Log打印:
    Thread.currentThread().getName=A
    Thread.currentThread().isAlive()true
    Thread.currentThread()==this :false
    this.getName()=Thread-0
    this.isAlive()=false
    说明此时的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()。

    因此我们修改下main中的代码为:
    public class Run {
    
        public static void main(String[] args){
    
            CountOperate c = new CountOperate();
            c.start();
        }
    }

    打印的log为:

    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

    与我们预想的结果相同





  • 相关阅读:
    跃迁方法论 Continuous practice
    EPI online zoom session 面试算法基础知识直播分享
    台州 OJ 2648 小希的迷宫
    洛谷 P1074 靶形数独
    洛谷 P1433 DP 状态压缩
    台州 OJ FatMouse and Cheese 深搜 记忆化搜索
    台州 OJ 2676 Tree of Tree 树状 DP
    台州 OJ 2537 Charlie's Change 多重背包 二进制优化 路径记录
    台州 OJ 2378 Tug of War
    台州 OJ 2850 Key Task BFS
  • 原文地址:https://www.cnblogs.com/huangyichun/p/6071625.html
Copyright © 2011-2022 走看看