synchronized and Lock What is the difference?

Copyright: Reprinted please note the name of the source https://blog.csdn.net/meism5/article/details/90413893

synchronized and Lock What is the difference?

  • Implementation level is not the same. synchronized keyword in Java, JVM level to achieve locking and releasing locks; Lock is an interface to achieve locking and releasing locks in the code level
  • Whether to automatically release the lock. the code execution thread synchronized automatically releases the lock when finished or abnormal; Lock does not automatically release the lock, need to finally {} block explicitly release the lock
  • Whether you have been waiting for. synchronized causes the thread can not get the lock has been waiting for; Lock can be set up to try to acquire locks or locking failure to obtain a certain timeout
  • Acquiring the lock success if known. synchronized not know whether to acquire a lock success; Lock can get locked whether successful tryLock
  • Functional complexity. synchronized reentrant lock, can not be interrupted, non-fair; Lock reentrant, it can be determined, be fair and unfair, read-write locks subdivided efficiency
     

 

 

More columns:

Guess you like

Origin blog.csdn.net/meism5/article/details/90413893