How does Thread.currentThread () work?

Thread.currentThread() is a static method that provides a link to the current executable thread (basically a link to the thread 'this').

Access to non-stationary members (especially this ) inside a static method in Java is not possible, therefore currentThread() is a native method.

How does the currentThread() method work behind the scenes?

+8
java multithreading
source share
1 answer
 (basically a reference to 'this' thread) 

Wrong. There are no this links here.

You mix the thread as your own resource, that is, the thread of execution; and Thread , which is a Java class. Thread code does not run β€œinside” the Thread instance, this instance is just your handle to Java thread control. Much like an instance of File not a file.

So, Thread.currentThread() is a way to get an instance of Thread that is responsible for the executable thread within which the method is called. How exactly does Java do this is an implementation detail that shouldn't bother you if you don't study the details of a specific JVM implementation.

+5
source share

All Articles