Java多线程之this与Thread.currentThread()的区别
<pre> <code class="language-java"> package mythread; 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"); } } </code></pre> <pre> <code class="language-java"> public class Run { public static void main(String[] args){ CountOperate c = new CountOperate(); c.start(); 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()); } } </code></pre> <p>打印的log为:</p> <pre> <code class="language-java">CountOperate---begin Thread.currentThread().getName()=main Thread.currentThread().isAlive()=true this.getName=Thread-0 this.isAlive()=false CountOperate---end Thread.currentThread()==this :false 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()=Thread-0 this.isAlive()=false run --- end </code></pre> <p>根据打印的Log可以知道调用CountOperate构造函数的是main线程,因此打印出</p> <pre> <code class="language-java">Thread.currentThread().getName()=main Thread.currentThread().isAlive()=true 而此时还没有启动CountOperate子线程所以打印出</code></pre> <pre> <code class="language-java">this.getName=Thread-0 this.isAlive()=false 此时this代表的是CountOperate对象实例,所以</code></pre> <pre> <code class="language-java">Thread.currentThread()==this :false 这里比较让人疑惑的是“this.getName() = Thread-0”,这个Thread-0是什么东西??? </code></pre> <pre> <code class="language-java">通过查看Thread源码发现,在Thread类的构造方法中,会自动给name赋值,赋值代码:</code></pre> <p><img src="https://simg.open-open.com/show/ad2c253030d7ada36ca306bf95fc5d6e.png"></p> <pre> <code class="language-java">然后执行到:</code></pre> <pre> <code class="language-java">Thread t1 = new Thread(c); System.out.println("main begin t1 isAlive=" + t1.isAlive()); t1.setName("A"); t1.start(); Log打印: Thread.currentThread().getName=A Thread.currentThread().isAlive()true Thread.currentThread()==this :false this.getName()=Thread-0 this.isAlive()=false 说明此时的this和Thread.currentThread()指向不是同一个线程实例 也就是说,this指向的还是new CountOperate()创建的那个线程实例,而不是new Thread(thread)创建的那个实例即t1。 查看源代码可以知道。 <img alt="" src="https://simg.open-open.com/show/0fdbdeea2b522baae2041fa16032733d.png"> 实际上new Thread(thread)会将thread应用的对象绑定到一个pravite变量target上, 在t1被执行的时候即t1.run()被调用的时候,它会调用target.run()方法,也就是说它是直接调用thread对象的run方法, 再确切的说,在run方法被执行的时候,this.getName()实际上返回的是target.getName(),而Thread.currentThread().getName()实际上是t1.getName()。 因此我们修改下main中的代码为: </code></pre> <pre> <code class="language-java">public class Run { public static void main(String[] args){ CountOperate c = new CountOperate(); c.start(); } } </code></pre> <p>打印的log为:</p> <pre> <code class="language-java">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 </code></pre> <p>与我们预想的结果相同</p> <p> </p> <p>来自:http://www.cnblogs.com/huangyichun/p/6071625.html</p> <p> </p>
本文由用户 user_ABCD 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!