Is it possible that the if block in the perfInit() method is executed multiple times?

Poison tianshuang.me at qq.com
Sat Feb 19 06:11:06 UTC 2022


When using JDK 8 on Linux, the following code: https://github.com/openjdk/jdk8u/blob/jdk8u332-b02/jdk/src/solaris/native/sun/management/LinuxOperatingSystem.c#L204-L227


When multiple threads call sun.management.OperatingSystemImpl#getProcessCpuLoad concurrently, whether the if block in the perfInit() method may be executed multiple times, although I don't know if it will affect the return value of the getProcessCpuLoad0() method, but it seems that this problem does exist, because there is no lock when getProcessCpuLoad0() is called.


Reference: https://www.quora.com/Why-use-of-local-static-variable-makes-C-code-not-thread-safe


More information about the jdk8u-dev mailing list