The difference between synchronized and lock

 

1. Synchronized is at the JVM level, and lock is an interface 
2. When an exception occurs, synchronized will automatically release the occupied lock; and lock must release the lock in finally, otherwise it may cause deadlock. 
3. During the lock waiting process, interrupt can be used to wait for the terminal, while synchronized can only wait for the release of the lock and cannot respond to the interrupt; 
4. Lock can know whether the lock has been acquired through trylock, but synchronized cannot; 
5. Synchronized is used for a small amount of synchronization The efficiency is similar to that of lock, and the efficiency of synchronized is lower when a large number of synchronizations occur.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324438026&siteId=291194637