J-J
J-J

Reputation: 5871

Difference between Thread.currentThread().getId() and Kernel32.INSTANCE.GetCurrentThreadId()

What is the difference between the following two lines of code to get the thread ID?

Thread.currentThread().getId();
Kernel32.INSTANCE.GetCurrentThreadId();

Upvotes: 1

Views: 2820

Answers (2)

Atul
Atul

Reputation: 2711

To abstract the underlying platform the jvm sits on top of the operating system. W32API is the OS interface/library which is implemented by java and the Kernel32.INSTANCE.GetCurrentThreadId() gives the id of the operating system thread calling that line. Java language provides mechanisms for multi-threading. The Thread.currentThread().getId() gives you the id of the jvm thread. Depending on implementation of W32API- of which Kernel32 is one- and maybe some other factors the OS threads may or may not map to the vm threads.

Upvotes: 1

Arjan
Arjan

Reputation: 823

Kernel32.INSTANCE.GetCurrentThreadId();

Retrieves the thread identifier of the calling thread. This is for native threads on Win32. They're scheduled by the operating system.

Thread.currentThread().getId();

Gets the id of the Java thread. They're scheduled by the jvm. Whether or not they are directly mapped to native threads depends on the jvm. (But usually, they are)

Upvotes: 1

Related Questions