Imagine a Java class with three methods:
master()
foo()
bar()
I want to synchronize master()
and foo()
and also master()
and bar()
, without synchronizing foo()
and bar()
. It can be done will a separate lock for every pair of synchronized methods, but my actual code has many more than three methods so I was hoping there's a way to do it without so many lock objects.
You are essentially describing a
ReadWriteLock
. Every two methods are allowed to run simultaneously (a "read lock"), except formaster()
, which excludes all others (a "write lock"):