zoukankan      html  css  js  c++  java
  • Java 多线程 ---- 线程中this与 Thread.currentThread()线程的区别

    总结起来一句话:在Thread中调用this其实就是调用Thread私有Runnable类型的target,target是Thread类的一个属性,而Thread.currentThread()是指新New出来的实例Thread类。两个是不同的对象。实例化一个Thread的对象,都会将其赋值给Thread的私有target属性。

    直接上代码:

    注意代码中红色部分,就可以解释this和Thread.currentThread()的区别。实际上new Thread(Runnable),new Thread(Runnable,String) 会将一个thread( Thread 是Runnable接口的实现类) 应用的对象绑定到一个pravite变量target上,在t1被执行的时候即t1.run()被调用的时候,它会调用target.run()方法,也就是说它是直接调用thread对象的run方法,再确切的说,在run方法被执行的时候,this.getName()实际上返回的target.getName(),而Thread.currentThread().getName()实际上是t1.getName()。

    new Thread(Runnable)的源码

    1  public Thread(Runnable target) {
    2         init(null, target, "Thread-" + nextThreadNum(), 0);
    3     }
     1 public class TestThread {
     2 
     3     public static void main(String[] args) {
     4         Bed b = new Bed();
     5         b.start();
     6         Thread t1 = new Thread(b,"B");
     7         System.out.println("main begin t1 isAlive=" + t1.isAlive());
     8 //        t1.setName("B");
     9         t1.start();
    10         System.out.println("main end t1 isAlive=" + t1.isAlive());
    11 
    12     }
    13 }
    14 
    15 class Bed extends Thread {
    16 
    17     public Bed() {
    18         System.out.println("Bed Constructor---begin");
    19         System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());// 获取线程名
    20         System.out.println("Thread.currentThread().isAlive()=" + Thread.currentThread().isAlive()); // 查看线程是否存活
    21         System.out.println("this.getName=" + this.getName());
    22         System.out.println("this.isAlive()=" + this.isAlive());
    23         System.out.println("Thread.currentThread()==this :" + (Thread.currentThread() == this));
    24         System.out.println("Bed Constructor---end ");
    25     }
    26 
    27     @Override
    28     public void run() {
    29         System.out.println("run---begin");
    30         System.out.println("Thread.currentThread().getName=" + Thread.currentThread().getName());
    31         System.out.println("Thread.currentThread().isAlive()" + Thread.currentThread().isAlive());
    32         System.out.println("Thread.currentThread()==this :" + (Thread.currentThread() == this));
    33         System.out.println("this.getName()=" + this.getName());
    34         System.out.println("this.isAlive()=" + this.isAlive());
    35         System.out.println("run --- end");
    36     }
    37 }

    结果:

     1 Bed Constructor---begin
     2 Thread.currentThread().getName()=main
     3 Thread.currentThread().isAlive()=true
     4 this.getName=Thread-0
     5 this.isAlive()=false
     6 Thread.currentThread()==this :false
     7 Bed Constructor---end 
     8 main begin t1 isAlive=false
     9 run---begin
    10 Thread.currentThread().getName=Thread-0
    11 Thread.currentThread().isAlive()true
    12 Thread.currentThread()==this :true
    13 this.getName()=Thread-0
    14 this.isAlive()=true
    15 run --- end
    16 main end t1 isAlive=true
    17 run---begin
    18 Thread.currentThread().getName=B
    19 Thread.currentThread().isAlive()true
    20 Thread.currentThread()==this :false
    21 this.getName()=Thread-0
    22 this.isAlive()=false
    23 run --- end

    b.start()的时候,就是调用Thread的run方法,而newThread对象的时候将其赋给target对象,如果未指定名称系统默认给target命名,target是线程类Thread的一个私有Runnable类型属性。

    1     private Runnable target;

    即使将target和new Thread的线程赋值一样的名称也不会出现this==Thread.currentThread(),从而说明target和new Thread()是两个不同的对象。

     1 public class TestThread {
     2 
     3     public static void main(String[] args) {
     4 
     5         Bed b = new Bed("A");
     6         b.start();
     7         Thread t1 = new Thread(b,"A");
     8         System.out.println("main begin t1 isAlive=" + t1.isAlive());
     9         t1.start();
    10         System.out.println("main end t1 isAlive=" + t1.isAlive());
    11 
    12     }
    13 
    14 }

    结果:

     1 Bed Constructor---begin
     2 Thread.currentThread().getName()=main
     3 Thread.currentThread().isAlive()=true
     4 this.getName=A
     5 this.isAlive()=false
     6 Thread.currentThread()==this :false
     7 Bed Constructor---end 
     8 run---begin
     9 Thread.currentThread().getName=A
    10 Thread.currentThread().isAlive()true
    11 Thread.currentThread()==this :true
    12 this.getName()=A
    13 this.isAlive()=true
    14 run --- end
    15 main begin t1 isAlive=false
    16 main end t1 isAlive=true
    17 run---begin
    18 Thread.currentThread().getName=A
    19 Thread.currentThread().isAlive()true
    20 Thread.currentThread()==this :false
    21 this.getName()=A
    22 this.isAlive()=false
    23 run --- end

    Reference

    [1] https://www.cnblogs.com/huangyichun/p/6071625.html

  • 相关阅读:
    KMP算法之查找模式串在源串中出现的次数
    快速排序算法分析
    排序算法的稳定性分析(转)
    动态规划之最优二叉搜索树(算法导论)
    动态规划之最长公共子序列(算法导论)
    动态规划原理(算法导论)
    动态规划之钢条切割(算法导论)
    动态规划之矩阵链相乘问题(算法导论)
    HNU 13064 Cuckoo for Hashing解题报告 North America
    HNU 13081 Even Up Solitaire解题报告
  • 原文地址:https://www.cnblogs.com/hoojjack/p/7940748.html
Copyright © 2011-2022 走看看