After Java SE5, a new lock interface (and related implementation classes) is added to the package to implement the lock function. Although it is less (through the synchronized block or the method provided) to obtain the convenience of the release lock implicitly, but it has the lock acquisition and release of the operational, interruptible acquisition lock and time-out to acquire locks and other synchronized keywords do not have the synchronization characteristics.
The use of the Synchronized keyword implicitly acquires the lock, but it solidifies the acquisition and release of the lock, which is the first fetch and release. Of course, this simplifies the management of synchronization, but extensibility does not show the lock to get and release well.
The key features that are not available with the Synchronized keyword provided by the lock interface are as follows:
Characteristics |
Describe |
Try to get the lock non-blocking |
The current thread attempts to acquire a lock, and if the lock is not acquired by another thread, the lock is successfully acquired and held |
can be interrupted to acquire the lock |
Unlike the synchronized keyword, a thread that acquires a lock can respond to an interrupt, and when the thread that acquires the lock is interrupted, the interrupt exception is thrown and the lock is freed |
Timeout Acquisition lock |
Locks are acquired before the established deadline, and if the closing time is still unable to acquire the lock, the return |
The lock--lock interface in Java