Synchronization vs Lock

java.util.concurrent api provides a class called as Lock, which would basically serializes the conntrol in order to access the critical resource. It gives method such as park() and unpark().

We can do similar things if we can use synchronized keyword and using wait() and notify() notifyAll() methods.

I am wondering which one of these is better in practice and why?



Lock is only on current Thread and It is not like classlevel or object level or method level. Lock will be applied on current Thread for specific block. Advantage with Lock is will have control on specific block within time period. Within the period that block will work like syncronize for current thread.

This will not be possible with Synchronize.

Synchronize will be locked untill execution of currentThread for other thread.

Post a Reply
Captcha Image