关于Thread.currentThread()和this的差异
重新来看多线程时,被这结果搞懵逼了。不多说,直接上代码:
public class MyThread02 extends Thread {
public MyThread02() {
System.out.println("init curr: " + Thread.currentThread().getName());
System.out.println("init this: "+this.getName());
}
@Override
public void run() {
System.out.println("run curr: " + Thread.currentThread().getName());
System.out.println("run this: "+this.getName());
}
}
public class Test02 {
public static void main(String[] args) {
MyThread02 target = new MyThread02();
Thread thread = new Thread(target);
thread.setName("A");
thread.start();
}
}
init curr: main
init this: Thread-0
run curr: A
run this: Thread-0
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
/* For autonumbering anonymous threads. */
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
return threadInitNumber++;
}
public void run() {
if (target != null) {
target.run();
}
}
关于Thread.currentThread()和this的差异的更多相关文章
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
package mythread; public class CountOperate extends Thread{ public CountOperate(){ System.out.prin ...
- ClassLoader,Thread.currentThread().setContextClassLoader,tomcat的ClassLoader
实际上,在Java应用中所有程序都运行在线程里,如果在程序中没有手工设置过ClassLoader,对于一般的java类如下两种方法获得的ClassLoader通常都是同一个 this.getClass ...
- Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream
Class.forName() 和 ClassLoader.loadClass()的区别? Class.forName() 和 Class.forName().NewInstance()的区别? Cl ...
- java 笔记 Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别
查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行.首先是执行Tomcat org.apache.catalina.startup ...
- String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) 获取项目的绝对路径(shiro项目中来的八)
一,上代码 String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) file: ...
- Not on FX application thread; currentThread = AWT-EventQueue-0的解决方法
swing awt跑javafx报了这问题 Not on FX application thread; currentThread = AWT-EventQueue-0 解决方法 Platform.r ...
- Thread.currentThread()和this的区别——《Java多线程编程核心技术》
前言:在阅读<Java多线程编程核心技术>过程中,对书中程序代码Thread.currentThread()与this的区别有点混淆,这里记录下来,加深印象与理解. 具体代码如下: pub ...
- Thread.currentThread().getContextClassLoader().getResourceAsStream
Thread.currentThread().getContextClassLoader().getResourceAsStream 2014年04月02日 06:49:47 OkidoGreen 阅 ...
- 演示Thread.sleep(100)和Thread.currentThread().isInterrupted()+@Deprecated:将方法标注为废弃的方法
package charpter08; public class TestInterrupt01 { public static void main(String[] args) { Processo ...
随机推荐
- Python :open文件操作,配合read()使用!
python:open/文件操作 open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) 如何打开文件 handle=open(file_name,a ...
- C代码工具--自动生成enum值和名字映射代码
这年头好像继续做C语言的人不多了,年轻人大多去互联网和移动应用.确实,那两个领域现在来钱快,且总是供不应求.就说刚刚在一个旧同事的微信群里,有人刚放出自己有团队可以做App几分钟,哇塞,好几个人说有项 ...
- http协议与网页优化泛谈
最近公司开展职业技能培训,本人招录了部分内容写成博客,若有纰漏,欢迎拍砖. http简述: http又称超文本传输协议,是一种无状态&无记忆协议的运用层协议,处于tcp传输层协议之上,采用请求 ...
- MongoDB学习笔记-06 数据库命令、固定集合、GridFS、javascript脚本
介绍MongoDB支持的一些高级功能: 数据库命令 固定大小的集合 GridFS存储大文件 MongoDB对服务端JavaScript的支持 数据库命令 命令的原理 MongoDB中的命令其实是作为一 ...
- 【原创】JAVA并发编程——Callable和Future源码初探
JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
- php中explode与split的区别介绍
php中explode与split的区别介绍 作者: 字体:[增加 减小] 类型:转载 今天在使用split时遇到一些问题.还是对函数理解不深刻,特写出来做个记 首先来看下两个方法的定义: 函数原型: ...
- 工具mark
http://zh.snipaste.com/ 截图工具 https://brookhong.github.io/2014/04/28/keycast-on-windows-cn.html 按键显示 ...
- 《Linux内核设计与实现》读书笔记 - 目录 (完结)
读完这本书回过头才发现, 第一篇笔记居然是 2012年8月发的, 将近一年半的时间才看完这本书(汗!!!). 为了方便以后查看, 做个<Linux内核设计与实现>读书笔记 的目录: < ...
- Building Apps for Windows Phone 8.1教程下载地址整理
官方教程地址http://channel9.msdn.com/Series/Building-Apps-for-Windows-Phone-8-1http://media.ch9.ms/ch9/8db ...
- 循序渐进做项目系列(1):最简单的C/S程序——让服务器来做加法
(本文是专门针对未接触过C/S开发的初学者而写的,C/S开发高手请自动忽略啊~~) 还在写“Hello world!”式的单机程序吗?还在各种拖控件吗?是否自己都觉得有点low呢?来个质的飞跃吧!看看 ...