admin管理员组文章数量:1612065
结论:
- Thread.currentThread():获取当前线程的引用,既代码段正在被哪一个线程调用。
- this:指当前对象
- this.currentThread():与Thread.currentThread()一致,都是调用
实验代码:
public class CountOperate extends Thread {
public CountOperate() {
System.out.println("=========CountOperate start============");
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("this.currentThread().getName() = " + this.currentThread().getName());
System.out.println("this.currentThread().isAlive() = " + this.currentThread().isAlive());
System.out.println("=========CountOperate end ============");
}
@Override
public void run() {
System.out.println("=========run start============");
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("this.currentThread().getName() = " + this.currentThread().getName());
System.out.println("this.currentThread().isAlive() = " + this.currentThread().isAlive());
System.out.println("=========run end ============");
}
}
实例一:
public class TestCountOperate {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
countOperate.start();
}
}
结果:
=========CountOperate start============
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
this.currentThread().getName() = main
this.currentThread().isAlive() = true
=========CountOperate end ============
=========run start============
Thread.currentThread().getName() = Thread-0
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = true
Thread.currentThread() == this true
this.currentThread().getName() = Thread-0
this.currentThread().isAlive() = true
=========run end ============
实例二:
public class TestCountOperate {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
Thread thread = new Thread(countOperate);
thread.setName("A");
thread.start();
}
}
结论:
=========CountOperate start============
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
this.currentThread().getName() = main
this.currentThread().isAlive() = true
=========CountOperate end ============
=========run start============
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
Thread.currentThread() == this false
this.currentThread().getName() = A
this.currentThread().isAlive() = true
=========run end ============
将线程对象countOperate 以构造参数的方式传递给Thread对象进行start()启动线程,我们直接启动的线程实际是thread,而作为构造参数的countOperate,赋给Thread类中的属性target,之后在Thread的run方法中调用target.run();此时Thread.currentThread()是Thread的引用thread, 而this依旧是CountOperate的引用,所以是不一样的,打印的内容也不一样。
本文标签: 多线程区别threadcurrentThread
版权声明:本文标题:多线程中this、Thread.currentThread() 和 this.currentThread()的区别 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728627551a1166689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论