Thread.currentThread().getName()与this.getName()的区别

一:代码示例

  1. 创建继承Thread类的CountOperate
      
    package com.kgf.test;
    
    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");
    	}
    	
    	@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("this.getName()="+this.getName());
    		System.out.println("this.isAlive()="+this.isAlive());
    		System.out.println("Thread.currentThread() == this : " + (Thread.currentThread() == this));
    		System.out.println("run---end");
    	}
    	
    }
    

     

  2. 创建测试类
         
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) {
    		CountOperate myThread = new CountOperate();
    		myThread.setName("A");
    		myThread.start();
    	}
    }
    

     

  3.   效果如下:
       
    CountOperate---begin
    Thread.currentThread().getName()=main
    Thread.currentThread().isAlive()=true
    this.getName()=Thread-0
    this.isAlive()=false
    CountOperate---end
    run---begin
    Thread.currentThread().getName()=A
    Thread.currentThread().isAlive()=true
    this.getName()=A
    this.isAlive()=true
    Thread.currentThread() == this : true
    run---end
    

     

  4. 分析原因
          ⑴先说构造方法中的代码结果
                a:
                         Thread.currentThread().getName()=main
                         Thread.currentThread().isAlive()=true

                        上面这个结果是我们在实例化CountOperate对象时,调用无参构造时产生的,
                        并且调用CountOperate构造方法是主线程main。
                b:
                         this.getName()=Thread-0
                         this.isAlive()=false

                         上面这个结果,首先我们需要搞清楚this代表的是什么?它代表的其实就是CountOperate类对象的引用,
                         是个线程类(它继承了Thread线程类),但是这个线程类并没有设置名字,所以Thread默认给了一个Thread-0,
                         默认名字的规则定义如下:
                         
                         因为仅仅是运行构造方法,还未运行线程,所以this.isAlive() = false
          ⑵再说run方法中的代码结果
                a:
                         Thread.currentThread().getName()=A
                         Thread.currentThread().isAlive()=true

                          当前线程名字为A,A是我们手动赋予的myThread.setName("A");,并且它是运行着的
                b:
                        this.getName()=A
                        this.isAlive()=true
                        Thread.currentThread() == this : true

                        因为,我们运行的线程就是MyThread的引用,而this也是MyThread的引用,所以相同,结果也是一样。
  5. 下面我们保持线程类不变,修改我们的测试类
          ⑴修改测试类
                 
    package com.kgf.test;
    
    public class Run {
    
    	public static void main(String[] args) {
    		CountOperate myThread = new CountOperate();
    		Thread thread = new Thread(myThread,"A");
    		thread.start();
    	}
    }
    

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

          ⑶先说构造方法中的代码结果
                  a:
                          Thread.currentThread().getName()=main
                          Thread.currentThread().isAlive()=true
                          this.getName()=Thread-0
                          this.isAlive()=false

                          上面这个结果是我们在实例化CountOperate对象时,调用无参构造时产生的,
                          并且调用CountOperate构造方法是主线程main。上面这个结果,首先我们需
                          要搞清楚this代表的是什么?它代表的其实就是CountOperate类对象的引用,
                         是个线程类(它继承了Thread线程类),但是这个线程类并没有设置名字,
                         所以Thread默认给了一个Thread-0
          ⑷再说run方法中的代码结果
                   a:
                          Thread.currentThread().getName()=A
                          Thread.currentThread().isAlive()=true
                          this.getName()=Thread-0
                          this.isAlive()=false
                          Thread.currentThread() == this : false

                        从上面我们会发现this 与 Thread.currentThread()不是同一个引用,
                        从测试类我们可以发现,我们启动线程的用的是thread对象,里面
                        我们的构造参数是myThread,赋给Thread类中的属性target,之
                        后在Thread的run方法中调用target.run();此时Thread.currentThread()
                        是Thread的引用newThread, 而this依旧是MyThread的引用,所以是
                        不一样的,打印的内容也不一样。        

你可能感兴趣的:(多线程)