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)的源码
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()线程的区别的更多相关文章
- java 多线程 :ThreadLocal 共享变量多线程不同值方案;InheritableThreadLocal变量子线程中自定义值,孙线程可继承
ThreadLocal类的使用 变量值的共享可以使用public static变量的形式,所有的线程都是用同一个public static变量.如果想实现每一个线程都有自己的值.该变量可通过Thr ...
- Java 多线程基础(十)interrupt()和线程终止方式
Java 多线程基础(十)interrupt()和线程终止方式 一.interrupt() 介绍 interrupt() 定义在 Thread 类中,作用是中断本线程. 本线程中断自己是被允许的:其它 ...
- Java多线程编程中Future模式的详解
Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Futu ...
- Java多线程编程中Future模式的详解<转>
Java多线程编程中,常用的多线程设计模式包括:Future模式.Master-Worker模式.Guarded Suspeionsion模式.不变模式和生产者-消费者模式等.这篇文章主要讲述Futu ...
- 【系列】Java多线程初学者指南(1):线程简介
原文地址:http://www.blogjava.net/nokiaguy/archive/2009/nokiaguy/archive/2009/03/archive/2009/03/19/26075 ...
- 2015年11月25 Java基础系列(二)Thread Runnable线程初级讲解
序,线程是比进程小的进程,非常广泛的被使用. 一.继承Thread实现线程操作 1.注意setDaemon(boolean)方法,参数为true时为守护线程,参数为false时为用户线程. 守护线程的 ...
- Java多线程初学者指南(4):线程的生命周期
与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. // 开始线程 ...
- “全栈2019”Java多线程第二十二章:饥饿线程(Starvation)详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
- “全栈2019”Java多线程第十二章:后台线程setDaemon()方法详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...
随机推荐
- 超强OCR文字识别软件首选ABBYY FineReader
提到纸质文档—转换—文本格式—可编辑这些字眼,相信大家的第一反映都是OCR文字识别软件,如何排除错误或利用辅助信息提高识别正确率,是OCR最重要的课题,衡量一个OCR系统性能好坏的主要指标无非是精确度 ...
- QT编译错误:Project ERROR: This example requires Qt to be configured with -opengl desktop
学习QT场景视图,对一个Boxes的例子比较感兴趣,于是去编译学习,结果编译不能通过(使用的是QT5.12): Project ERROR: This example requires Qt to b ...
- Tomcat 8
JDTCompiler.java /** * Compile the jsp file from the current engine context. As an side- effect, * t ...
- Linux+Redis实战教程_day02_3、redis数据类型_4、String命令_5、hash命令_6、java操作redis数据库技术
3. redis数据类型[重点] redis 使用的是键值对保存数据.(map) key:全部都是字符串 value:有五种数据类型 Key名:自定义,key名不要过长,否则影响使用效率 Key名不要 ...
- UITextField中文输入法输入时对字符长度的限制 输入时对字符类型的限制
检索一个字符串的长度的话:直接用 length,去进行判断就行了, 如果要检索字符串是否是自己要限制的类型的话,可以用正则表达式: 举个例子: 匹配9-15个由字母/数字组成的字符串的正则表达式: ...
- 在python中重新导入模块
重新加载模块 倘若,更改了已经在 Python shell 中导入的模块,然后重新导入该模块,Python 会认为“我已经导入了该模块,不需要再次读取该文件”,所以更改将无效. 要解决这个问题,有以下 ...
- linux下kill -9 pid 强制不能杀掉进程原因
今天安装集群的时候,发现一个进程一直存在,kill -9 pid 也干不掉,就找找原因了. kill -9发送SIGKILL信号将其终止,但是以下两种情况不起作用:a.该进程处于"Zomb ...
- iOS开发-编译出错 duplicate symbols for architecture x86_64
今天对原来项目文件进行重新整理,根据文件内容进行分类,结果复制粘贴时没注意把一个文件复制了两遍 编译的时候就出现Duplicate Symbol Error 在网上搜素了一圈发现也有人遇到过这个问题, ...
- Ansible 实战:一键安装 LNMP
Ansible 配置文件 : [root@center /data/ansiblework]# cat ansible.cfg [defaults] remote_user = root remote ...
- NSIS安装vcredist_64.exe
; ExecWait ‘vcredist_x86.exe’ # 一般的安装ExecWait ‘”vcredist_x86.exe” /q’ # silent install 静默安装; ExecWai ...