1.

What is the lock interface? Why is it better to use a lock interface rather than a synchronized block.?

Answer»

Lock interface was introduced in Java 1.5 and is generally used as a synchronization mechanism to provide important OPERATIONS for BLOCKING.  

Advantages of using Lock interface over Synchronization BLOCK

  • Methods of Lock interface i.e., Lock() and UNLOCK() can be called in different methods. It is the main advantage of a lock interface over a synchronized block because the synchronized block is FULLY contained in a single method.  
  • Lock interface is more flexible and makes sure that the longest waiting thread gets a fair chance for execution, unlike the synchronization block.


Discussion

No Comment Found