Gilgamesz
Gilgamesz

Reputation: 5073

Controlling system threads by JVM

I'm reading about a garbage collector in JVM. And it is written that in some phases of the collecting the applications threads are stopped.

I am not sure how threads can be stopped by JVM (the owner of threads). I am not sure how a JVM thread can control created thread. I see that java process is owner of that thread but to that day I didn't see a method to stop running thread. I suppose and I assumed implicitly that Java wrap Unix threads. (please explain this issue because I am not sure).

Upvotes: 0

Views: 168

Answers (2)

Holger
Holger

Reputation: 298539

The JVM can control the threads, because the threads cooperate. There are only a few possibilities:

  1. The thread may be executing code of the JVM itself, e.g. the interpreter. Then, obviously, that code can contain checks whether thread suspension is needed.

  2. It may execute generated code, result of compiling byte code to native code. Since the JVM’s code generator produced that code, it cared to insert the necessary code to support thread suspension at “safe points”.

  3. It may execute arbitrary native code not supporting thread suspension. In that case, it doesn’t matter, as that code doesn’t interfere with services like garbage collection. The only way to access the Java heap, is through interfaces provided by the JVM, like JNI. Of course, as soon as the native code calls into a JVM provided function to access the heap, the JVM has the control over the operation and might suspend the thread until the end of an ongoing garbage collection.

The exact mechanics, e.g. how a safepoint will be implemented on the low level, are not important to understand that the JVM always has the control over all heap accesses which may interfere with processes like garbage collection. And only those threads need to be stopped.

Upvotes: 2

skrtbhtngr
skrtbhtngr

Reputation: 2251

If by "system threads" you refer to the threads outside the JVM, this is not possible!

Otherwise,

Safepoints!

From openjdk HotSpot glossary:

A point during program execution at which all GC roots are known and all heap object contents are consistent. From a global point of view, all threads must block at a safepoint before the GC can run.

From a local point of view, a safepoint is a distinguished point in a block of code where the executing thread may block for the GC.

It can be simply thought of as a point where the states of the running threads are well-defined, and they are ready to be blocked (if they weren't already).

Upvotes: 0

Related Questions