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
    复制代码

    与我们预想的结果相同

     转载自:http://www.cnblogs.com/huangyichun/p/6071625.html

  • 相关阅读:
    SpringBoot打包的程序部署到服务器后一直在后台运行
    ubuntu搭建mysql数据库
    解决ubuntu16.04 ‘E: 无法获得锁 /var/lib/dpkg/lock-frontend
    项目部署篇之——下载安装Xftp6,Xshell6
    linux 文件 chgrp、chown、chmod
    linux 正确的关机方法
    linux 常用命令
    spring 事务
    Spring 中 ApplicationContext 和 BeanFactory 的区别
    java 异常处理
  • 原文地址:https://www.cnblogs.com/Hennessy-Road/p/6211798.html
Copyright © 2011-2022 走看看