当前位置:网站首页>[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock

[multi thread performance tuning] multi thread lock optimization (Part 1): optimization method of synchronized synchronization lock

2022-06-24 20:30:00 SugarPPig

  JVM stay JDK1.6 A hierarchical locking mechanism is introduced to optimize Synchronized, When a thread acquires a lock , First, the object lock will become a biased lock , This is to optimize the switching between user state and kernel state caused by repeated acquisition of the same thread ; Secondly, if there are multiple threads competing for lock resources , Locks will be upgraded to lightweight locks , It is suitable for holding a lock for a short time , And there is a scene of alternating switching in locking ; The biased lock also uses a spin lock to avoid frequent switching between the thread user state and the kernel state , The system performance is greatly improved ; But if the lock competition is too fierce , Then the synchronization lock will be upgraded to a heavyweight lock .

   Reduce lock competition , It's optimization Synchronized The key to synchronization lock .

  1. Try to make Synchronized Synchronization lock is in lightweight lock or deflection lock , Improve Synchronized Synchronization lock performance ;
  2. Reduce lock granularity To reduce lock competition is also one of the most commonly used optimization methods ;
  3. adopt Reduce lock holding time To improve the Synchronized The success rate of synchronous lock to acquire lock resource when spinning , avoid Synchronized The synchronization lock is upgraded to a heavyweight lock .
原网站

版权声明
本文为[SugarPPig]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211327236699.html