Home > Software design >  What is the role of Threads_lock in jvm and which scenarios will be used
What is the role of Threads_lock in jvm and which scenarios will be used

Time:02-22

I met a problem: In management.cpp, the jmm_GetThreadInfo method will first acquire the Threads_lock lock, assuming that there are too many threads and it is very time-consuming, which leads to a non-obvious problem: some other threads are blocked and have been waiting for this lock to be released, but I can't find where the thread will be triggered suddenly blocked on this lock.

I have some questions:

  1. the role of Threads_lock in jvm and which scenarios will be used.
  2. whether the Threads_lock will cause other threads to block.

Threads_lock is in the mutexLocker.cpp.The official explanation is

a lock on the Threads table of active Java threads
(also used by Safepoints too to block threads creation/destruction)

thranks

CodePudding user response:

Threads_lock protects a list of JVM managed threads from concurrent modification. The lock is acquired when a new thread starts, when a thread exits, and at safepoints. Threads_lock does not block running threads from doing their regular work.

In JDK 8, Threads_lock was also acquired for iterating over a list of existing threads, and this indeed caused performance issues. Since JDK 10, JVM maintains thread list snapshot for lock-free read access.

  • Related