Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of new Lock interface over synchronized block in Java?

Tags:

java

What is the advantage of new Lock interface over synchronized block in Java? You need to implement a high performance cache which allows multiple reader but single writer to keep the integrity how will you implement it?

like image 397
Anu Avatar asked Nov 06 '11 08:11

Anu


People also ask

Why are locks better than synchronized?

Only one thread is allowed to access only one method at any given point of time using a synchronized block. This is a very expensive operation. Locks avoid this by allowing the configuration of various locks for different purpose.

What is lock how it is different from using synchronized approach?

Major difference between lock and synchronized: with locks, you can release and acquire the locks in any order. with synchronized, you can release the locks only in the order it was acquired.

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

Lock interface handles it. No timeout − Synchronized block has no option of timeout if lock is not granted. Lock interface provides such option. Single method − Synchronized block must be fully contained within a single method whereas a lock interface's methods lock() and unlock() can be called in different methods.

What is the advantage of synchronized block over synchronized method?

Synchronized blocks provide granular control over a lock, as you can use arbitrary any lock to provide mutual exclusion to critical section code. On the other hand, the synchronized method always locks either on the current object represented by this keyword or class level lock, if it's a static synchronized method.


2 Answers

The advantages of a lock are

  • it's possible to make them fair
  • it's possible to make a thread responsive to interruption while waiting on a Lock object.
  • it's possible to try to acquire the lock, but return immediately or after a timeout if the lock can't be acquired
  • it's possible to acquire and release locks in different scopes, and in different orders

Note that this is explained in the javadoc of Lock and its subclasses.

A high performant cache could be implemented using a ConcurrentMap.

like image 119
JB Nizet Avatar answered Sep 16 '22 19:09

JB Nizet


The various advantages of lock interface over synchronisation are listed below

  1. Synchronisation is the only culprit that leads to the problem of deadlock unlike lock which is free of deadlock issue.

  2. In synchronisation , we don’t know after how much time a thread will get a chance after a previous thread has released the lock. This can lead to problem of starvation whereas incase of lock we have its implementing class reentrant lock which has one of its constructor which lets you pass fairness property as one of its argument that leta longest waiting thread get the chance to acquire the lock.

  3. In synchronisation, if a thread is waiting for another thread, then the waiting thread won’t do any other activity which doesn’t require lock access but with lock interface there is a trylock() method with which you can try for access the lock and if you don’t get the lock you can perform other alternate tasks. This helps to improve the performance of the application .

  4. There is no api to check how many threads are waiting for a particular lock whereas this is possible with lock interface implementation class ReentrantLock methods.

  5. One can get better control of locks using lock interface with holdCount() method which is not found with synchronization.

like image 29
Aditya Avatar answered Sep 20 '22 19:09

Aditya