总结起来一句话:在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)的源码

  public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
 public class TestThread {

     public static void main(String[] args) {
Bed b = new Bed();
5 b.start();
6 Thread t1 = new Thread(b,"B");
System.out.println("main begin t1 isAlive=" + t1.isAlive());
// t1.setName("B");
t1.start();
System.out.println("main end t1 isAlive=" + t1.isAlive()); }
} class Bed extends Thread { public Bed() {
System.out.println("Bed Constructor---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("Bed Constructor---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("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");
}
}

结果:

 Bed Constructor---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=Thread-0
this.isAlive()=false
Thread.currentThread()==this :false
Bed Constructor---end
main begin t1 isAlive=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
main end t1 isAlive=true
run---begin
Thread.currentThread().getName=B
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false
this.getName()=Thread-0
this.isAlive()=false
run --- end

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

     private Runnable target;

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

 public class TestThread {

     public static void main(String[] args) {

         Bed b = new Bed("A");
b.start();
Thread t1 = new Thread(b,"A");
System.out.println("main begin t1 isAlive=" + t1.isAlive());
t1.start();
System.out.println("main end t1 isAlive=" + t1.isAlive()); } }

结果:

 Bed Constructor---begin
Thread.currentThread().getName()=main
Thread.currentThread().isAlive()=true
this.getName=A
this.isAlive()=false
Thread.currentThread()==this :false
Bed Constructor---end
run---begin
Thread.currentThread().getName=A
Thread.currentThread().isAlive()true
Thread.currentThread()==this :true
this.getName()=A
this.isAlive()=true
run --- end
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()=A
this.isAlive()=false
run --- end

Reference

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

Java 多线程 ---- 线程中this与 Thread.currentThread()线程的区别的更多相关文章

  1. java 多线程 :ThreadLocal 共享变量多线程不同值方案;InheritableThreadLocal变量子线程中自定义值,孙线程可继承

      ThreadLocal类的使用 变量值的共享可以使用public static变量的形式,所有的线程都是用同一个public static变量.如果想实现每一个线程都有自己的值.该变量可通过Thr ...

  2. Java 多线程基础(十)interrupt()和线程终止方式

    Java 多线程基础(十)interrupt()和线程终止方式 一.interrupt() 介绍 interrupt() 定义在 Thread 类中,作用是中断本线程. 本线程中断自己是被允许的:其它 ...

  3. Java多线程编程中Future模式的详解

    Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Futu ...

  4. Java多线程编程中Future模式的详解<转>

    Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Futu ...

  5. 【系列】Java多线程初学者指南(1):线程简介

    原文地址:http://www.blogjava.net/nokiaguy/archive/2009/nokiaguy/archive/2009/03/archive/2009/03/19/26075 ...

  6. 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解

    序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...

  7. Java多线程初学者指南(4):线程的生命周期

    与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. // 开始线程 ...

  8. “全栈2019”Java多线程第二十二章:饥饿线程(Starvation)详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  9. “全栈2019”Java多线程第十二章:后台线程setDaemon()方法详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

随机推荐

  1. golang interface的使用和实现(翻译整理)

    https://blog.csdn.net/u011409801/article/details/79291221

  2. [Unity官方文档翻译]Primitive and Placeholder Objects Unity原生3D物体教程

    Primitive and Placeholder Objects 原始的基础物体 Unity can work with 3D models of any shape that can be cre ...

  3. java.lang.Class<T> -- 反射机制及动态代理

    Interface : Person package java_.lang_.component.bean; public interface Person { String area = " ...

  4. Python函数相关

    Python中的函数也是一种对象,而且函数还是一等公民.函数能作为参数,也能作为返回值,这使得Python中的函数变得很灵活.想想前面两篇中介绍的通过内嵌函数实现的装饰器和闭包. 下面就介绍一下Pyt ...

  5. ios开发之-- 延迟执行方法

    延迟执行的几种方法,分享一下. 1.performSelector(NSObject)方法  2.NSTimer方法  3.GCD方法  4.sleep(NSThread)方法 1.performSe ...

  6. 关于openssl的编译与使用

    关于openssl的编译与使用,可以参考这两往篇文章 http://blog.csdn.net/lazyclough/article/details/7456131 http://www.leaves ...

  7. iOS开发--关闭ARC

    对整个项目关闭ARC project -> Build settings -> Apple LLVM complier 3.0 - Language -> objective-C A ...

  8. [Python]小百合十大爬虫

    国庆几天在家看了几篇关于使用Python来编写网络爬虫的博客,想来自己断断续续学习Python也有几个月了,但一个像样的程序都没有写过,编程能力并没有得到提高,愧对自己花费的时间.很多时候虽然知道什么 ...

  9. 嵌入式系统之ubootENV环境变量

    从bootm 命令讲起 1 找到linux的内核入口 Bootm命令通过读取uImage的头部0×40字节的信息,将uImage定位到正确的地址,同时找到linux的内核入口地址. 这个地方就涉及到u ...

  10. Bootstrap - select2

    1.调整select2下拉框的宽度 <style> .select2-container .select2-choice { height: 28px; line-height: 28px ...