8

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?

Anu
  • 573
  • 1
  • 5
  • 4

4 Answers4

14

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    Point two seems badly worded. You can interrupt a thread waiting on a normal intrinsic Java monitor. `Lock` has `lockInterruptibly` which allows the thread to be interrupted whilst blocked acquiring a lock. – Tom Hawtin - tackline Nov 06 '11 at 11:50
  • 1
    @Tom: you may of course interrupt a thread blocked waiting for an intrinsic monitor, but the thread won't be responsive to the interruption. That's what I meant: the interrupt method will be called, but the thread won't be able to interrupt itself until it acquires the lock, and it could stay in this state forever. I've changed the wording to make it more explicit. – JB Nizet Nov 06 '11 at 11:59
  • The key point is that the target thread is in `Thread.State.BLOCKED` not `Thread.State.WAITING` (or `TIMED_WAITING`). – Tom Hawtin - tackline Nov 06 '11 at 12:55
3

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.

Aditya
  • 950
  • 8
  • 37
0

The major advantage of lock interfaces on multi-threaded and concurrent programming is they provide two separate lock for reading and writing which enables you to write high-performance data structure like ConcurrentHashMap and conditional blocking.

A thread can take a lock only once. Synchronized blocks don’t offer any mechanism of a waiting queue and after the exit of one thread, any thread can take the lock. This could lead to starvation of resources for some other thread for a very long period of time.

-4

You need to know when to use Lock and when to use synchronized blocks/methods.

  • Use Synchronized blocks if you are creating simple applications. It avoids race conditions. But while avoiding race conditions you might cause deadlocks.

  • Use Locks if you are creating serious applications. It also avoids race conditions but you also have the benefit of avoiding deadlocks.

John Eipe
  • 10,922
  • 24
  • 72
  • 114
  • 1
    This is really not the good keys to choose between synchronized and explicit locks. A serious application may be simple, and using locks can obviously also lead to deadlocks as synchronized does. – JB Nizet Nov 06 '11 at 09:30