Article From:

synchronized The thread is locked, and other threads wait to know that the thread releases the lock. If an abnormal JVM occurs, the lock will be automatically released. If the thread waits for IO or the sleep () method does not release the lock, it will have to wait.

Interrupt lock: interrupt can be interrupted, lock can be interrupted while synchronized can not be interrupted.

Because the waiting time is too long, we would like to interrupt the next thread.   

Multiple threads are just reading operations, so when a thread is reading operations, other threads can only wait for no reading operation.

Therefore, a mechanism is needed to enable multiple threads to read only when there is no conflict between threads, and can be done through Lock.

Through Lock, you can know whether the thread has successfully acquired the lock. This is what synchronized can’t do

LockIt is necessary to release the lock actively, generally using unlock () in the finally block; and Lock must have the user to release the lock manually, and if the lock is not released actively, it may cause the phenomenon of deadlock.

Leave a Reply

Your email address will not be published. Required fields are marked *