How does Thread.currentThread() work?

前端 未结 1 1380
醉话见心
醉话见心 2021-01-11 10:09

Thread.currentThread() is a static method which provides reference to currently executing Thread (basically a reference to \'this\' thread).

相关标签:
1条回答
  • 2021-01-11 10:19
    (basically a reference to 'this' thread)
    

    There are no this references involved here.

    You are mixing up a thread as a native resource, meaning the thread of execution; and Thread, which is a Java class. Thread code does not run "within" the Thread instance, that instance is just your handle into Java's thread control. Much like a File instance is not a file.

    So, Thread.currentThread() is a way for you to retrieve the instance of Thread in charge of the thread-of-execution inside which the method is called. How exactly Java does this is an implementation detail which should not be your concern unless you are exploring the details of a particular JVM implementation.

    0 讨论(0)
提交回复
热议问题