Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Synchronization vs Lock

java.util.concurrent API provides a class called as Lock, which would basically serialize the control 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?

Answer*

Cancel
2
  • I liked the details on your comment. I would add one more bullet point - the ReadWriteLock provides useful behavior if you are dealing with several threads, only some of which need to write to the object. Multiple threads can be concurrently reading the object and are only blocked if another thread is already writing to it. Commented Jul 3, 2017 at 14:24
  • To add to the 4th point - In j.u.c.ArrayBlockingQueue, the lock has 2 reasons to await: queue non-empty and queue non-full. For this reason, j.u.c.ArrayBlockingQueue uses the explicit lock and lock.newCondition().
    – yiksanchan
    Commented Nov 30, 2020 at 16:52