E.g. method public static void sleep(long millis). This method causes current thread to sleep, but how does it know, which thread is current? Static methods are object-independent and belong to class, so how does this mechanism work?
E.g. method public static void sleep(long millis) . This method causes current thread to
Share
The current thread is managed by the underlying operating system (or the threading system). The underlying (system dependant) thread implementation provides a handle to the current thread:
In case of POSIX threads, there is the API call pthread_self() which returns the thread ID of the currently executing thread. You can think of
Thread.currentThread()eventually calling this C function and wrap the thread ID in a JavaThreadobject which is then returned.In case of MS Windows, there is the API call GetCurrentThread() which returns the Windows Handle of the currently executing thread. Again, you can think of
Thread.currentThread()eventually calling this C function and wrap the Handle in a JavaThreadobject which is returned.