The recent use of thread synchronization, the idea is not very clear, only remember synchronized and lock, the difference between what is not clear. Take a little space and summarize:
The lock interface basically has all the features of the synchronized
There are also three forms of lock acquisition (interruptible, non-disruptive, and timed) that may vary in their performance characteristics, sequencing guarantees, or other implementation quality.
such as threads A and b are to get the lock of the object o, a thread first obtains the lock of the object o, then B will wait for a to release the lock on O
If you are using synchronized, if a does not release the lock, B will wait until a unlocks the O
If you are using lock, if a does not release the lock, B can wait for a period of time to break the wait and do something else
Speed, in the case of less competition, synchronized faster thanlock, the competition is fierce in the case of lock faster than synchronized.
The synchronized is implemented on the JVM, and if an exception is thrown, the JVM will automatically release the lock.
Lock is a code implementation that requires manual handling of exception release locks
Java thread Synchronization