What mechanism JVM use to block threads during stop-the-world pause

后端 未结 1 603
谎友^
谎友^ 2021-02-07 05:15

I heard this question on an interview and couldn\'t provide an answer. Later I searched thru the internet and still didn\'t find an answer. Can anybody tell me how JVM stops thr

相关标签:
1条回答
  • 2021-02-07 05:54

    For HotSpot and OpenJDK at least, the JVM uses safe points to stop the application's flow in each thread, either introduced in the JITed code or by changing the bytecode mappings for interpreted code (see this post by Alexey Ragozin for more details).

    See also this answer by Gil Tene on why safepointing can be an additional issue when dealing with stop-the-world pauses.


    Here are more details (as I understand them, I don't claim to be an expert) on the safepointing mechanism in Hotspot/OpenJDK (see for example safepoint.cpp, line 154), based on the above resources, and probably on some articles by Cliff Click on the Azul Systems blog (which seems to have disappeared from the site).

    Reaching the safepoint

    The JVM needs to get control of the flow from the application, so it depends on the current state of the threads:

    • Blocked

      The JVM already has control of that thread.

    • Running interpreted code

      The interpreter goes into a mode (by swapping its dispatch table) where each bytecode evaluation will be preceded by a safepoint check.

    • Running native code (JNI)

      JNI code runs in a safepoint and can continue running, unless it calls back into Java or calls some specific JVM methods, at which point it may be stopped to prevent leaving the safepoint (thanks Nitsan for the comments).

    • Running compiled code

      The JVM makes a specific memory page (the Safepoint Polling page) unreadable, which makes the periodic reads of that page (inserted into the compiled code by the JIT compiler) fail and go to a JVM handler.

    • Running in the VM or transitioning states (in the VM also)

      The flow goes through the safepoint check at some point anyway, so the VM waits for it.

    Stopping at the safepoint

    Once a thread is at the safepoint, controlled by the JVM, the JVM simply blocks it from exiting. When all threads have been stopped (i.e. the world is stopped), the JVM can do the garbage collection then release all the threads which resume execution.

    For much more details, you can go read this blog post on safepoints written by Nitsan Wakart in the meantime (which itself has even more references).

    0 讨论(0)
提交回复
热议问题