synchronized with the difference ReentrantLock

ReentrantLock are synchronized with reentrant lock , before the difference between the two, to introduce the concept of reentrant lock.

Reentrant lock

The same thread can not release the lock on an object, the object can acquire the lock again. Each time the same thread acquires the lock, the lock count is incremented, once the lock is released, the counter is decremented by one, until the counter is zero, fully release the lock.

synchronized with the difference ReentrantLock

  • ReentrantLock is a class, flexible, must fit the lock is released
  • ReentrantLock manual acquire or release locks
  • ReentrantLock lock only for code block, not only for the synchronized code block, but also class, variable
  • Lock mechanism is not the same. Unsafe ReentrantLock call the park lock, synchronized using the object mark word

Incidentally different synchronized action, which corresponds to what is the lock?

  • Common methods: lock the current instance
  • Static method: class object lock the current class
  • Block Method: Lock object brackets

 

Guess you like

Origin www.cnblogs.com/ivy-xu/p/12586140.html