Java多线程Thread.currentThread()的疑问
高洛峰
高洛峰 2017-04-18 10:01:15
0
4
777
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (4)
PHPzhong

Thread.currentThread() does not return the thread in which the current program is running, but returns the thread in which the code Thread.currentThread() is executed.

So you can see that when the new thread is constructed, the code is executed in the main thread, while the code in the run method is executed in the thread.

It’s easy to understand by looking at the output

    Ty80

    1.new一个Thread对象的时候默认的名字就是Thread-n格式的,你可以看看Thread源码。
    2、你这就是一个线程对象,this在你这样使用的情况下,是当前的线程了。
    3、Thread.currentThread()Always returns the currently running thread.

      巴扎黑

      The running result of the constructor method is understandable, but why is the running result of the run method not testThread in setName but thread?

        洪涛

        1. The getName() method is inherited from the Thread class. Just look at the getName() method of the Thread class:

        /** * Returns this thread's name. * * @return this thread's name. * @see #setName(String) */ public final String getName() { return String.valueOf(name); }

        2.Yes
        3.Yes
        In the constructor, Thread.currentThread() is the main thread, and Thread.currentThread() in the run method is the current thread

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!