JAVA多线程编程之Thread中This和Thread.CurrentThread的区别
2017-04-25 15:53
495 查看
JAVA多线程编程之Thread中This和Thread.CurrentThread的区别
this代表当前的线程类,,[b]Thread.CurrentThread代表正在执行的线程,看例子就懂了:[/b]
1 自定义线程类
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");
}
}
2主函数
public class Run {
public static void main(String[] args){
CountOperate c = new CountOperate(); //创建自定义的线程类
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());
}
}
3打印的Log为
CountOperate---begin
Thread.currentThread().getName()=main //主线程调用构造
Thread.currentThread().isAlive()=true //主线程存活
this.getName=Thread-0 //this代表这个线程对象CountOperate
的名字 没给定义系统默认定义Thread-0
this.isAlive()=false //这个自定义线程CountOperate
没启动 不是活动
CountOperate---end
Thread.currentThread()==this :false //当前运行的线程是主线程 this代表CountOperate
肯定不相等
main begin t1 isAlive=false //t!没启动 为不活动状态
main end t1 isAlive=true //启动为活动状态
run---begin
Thread.currentThread().getName=A //t1.setName("A"); t1调用的run方法 给t1设置线程的名字
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false //正在运行的是t1 this还是自定义那个线程类
this.getName()=Thread-0
this.isAlive()=false
run --- end
说明此时的this和Thread.currentThread()指向不是同一个线程实例
5修改代码
只启动自定义的线程
public class Run {
public static void main(String[] args){
CountOperate c = new CountOperate();
c.start();
}
}
更改后的打印结果
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
this代表当前的线程类,,[b]Thread.CurrentThread代表正在执行的线程,看例子就懂了:[/b]
1 自定义线程类
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");
}
}
2主函数
public class Run {
public static void main(String[] args){
CountOperate c = new CountOperate(); //创建自定义的线程类
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());
}
}
3打印的Log为
CountOperate---begin
Thread.currentThread().getName()=main //主线程调用构造
Thread.currentThread().isAlive()=true //主线程存活
this.getName=Thread-0 //this代表这个线程对象CountOperate
的名字 没给定义系统默认定义Thread-0
this.isAlive()=false //这个自定义线程CountOperate
没启动 不是活动
CountOperate---end
Thread.currentThread()==this :false //当前运行的线程是主线程 this代表CountOperate
肯定不相等
main begin t1 isAlive=false //t!没启动 为不活动状态
main end t1 isAlive=true //启动为活动状态
run---begin
Thread.currentThread().getName=A //t1.setName("A"); t1调用的run方法 给t1设置线程的名字
Thread.currentThread().isAlive()true
Thread.currentThread()==this :false //正在运行的是t1 this还是自定义那个线程类
this.getName()=Thread-0
this.isAlive()=false
run --- end
4 这里比较让人疑惑的是“this.getName() = Thread-0”,这个Thread-0是什么东西???
通过查看Thread源码发现,在Thread类的构造方法中,会自动给name赋值,赋值代码:
说明此时的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()。
5修改代码
只启动自定义的线程
public class Run {
public static void main(String[] args){
CountOperate c = new CountOperate();
c.start();
}
}
更改后的打印结果
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
相关文章推荐
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
- Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术
- [置顶] Java多线程--详解this与Thread.currentThread()的区别
- this与Thread.currentThread()的区别——java多线程编程核心技术
- this和Thread.currentThread的区别
- Thread.currentThread().getName()与this.getName()的区别
- 多线程之 this与Thread.currentThread()的区别
- Java多线程中this.getName()和Thread.currentThread.getName()的区别?
- currentThread().getName和this.getName()的区别
- Thread.currentThread().getName() 和 this.getName()区别详解
- 并发this.getName()和Thread.currentThread().getName()区别
- Java 多线程 ---- 线程中this与 Thread.currentThread()线程的区别
- 【java】多线程中Thread.currentThread().getName()和this.getName()的区别
- thrd = new Thread(this);和thrd = Thread.currentThread();的区别。。。。
- 06_多线程中的This和Thread.currentThread()的区别
- Thread.currentThread().getName() ,对象实例.getName() 和 this.getName()区别
- Thread.currentThread()与this的区别
- Thread.currentThread()与this的区别
- Thread.currentThread()与this的区别