Ankush G
Ankush G

Reputation: 1081

Why java takes overhead of thread switching when OS is acting on behalf

Okay I learned that java consults OS for thread creation, i mean java uses OS API to create threads for multiprocessor CPU. So if OS is managing threads on behalf of JAVA then why java takes other overhead of thread management like storing thread frames and thread locals on stack and other thread related activities. Why can't OS alone takes care of it and let java not to care about anything related to threads.

Upvotes: 0

Views: 149

Answers (2)

Gray
Gray

Reputation: 116878

why java takes other overhead of thread management like storing thread frames and thread locals on stack and other thread related activities. Why can't OS alone takes care of it and let java not to care about anything related to threads.

There is a bunch of information that Java needs to store about threads that is OS independent. The thread name, daemon status, current lock monitors, etc.. It certainly offloads the actual scheduling and running of the thread to the OS but it needs to do a lot of administration of the threads to make sure that they work in concert with the JVM.

If you are talking more specifically of stack storage, then, as @the8472 states, most likely the JVM is making use of the native thread stack and doesn't create its own stack frames, etc..

Upvotes: 0

the8472
the8472

Reputation: 43052

then why to keep thread related stuff both in JAVA's virtual thread stack and also in OS native thread stack.

The abstract virtual machine described by the java language specification does involve a stack, yes. But that does not mean that real implementations use a stack that is separate from the native thread stack.

I.e. there is nothing that says an implementation has to use a native stack and some separate managed stacks.

Upvotes: 1

Related Questions