Home > Back-end >  Synchronized is useless to the mutex lock
Synchronized is useless to the mutex lock

Time:02-10

Don't know why the Internet to see the introduction of the said heavy lock synchronized with the underlying relies on the mutex lock, there will be a user mode and kernel mode switch, but in fact the JVM hotspot source no use mutex lock!
Lock:
 int ObjectMonitor: : TryLock (Self) Thread * {
For (;; ) {
Void * own=_owner;
If (own!=NULL) return 0;//if there is a thread owns a heavyweight lock, exit
//amend the _owner CAS operation for the current thread, operation success return> 0
If (Atomic: : cmpxchg_ptr (Self, & amp; _owner, NULL)==NULL) {
Return 1;
}
//CAS return< update failure; 0
If (true) return 1;
}
}


No competition to lock the thread will hang, this part do have kernel mode switch, but definitely not used to the mutex lock
Don't know if I understand right for help you a great god
  • Related