I've stumbled upon this thread some time ago: Does making a reentrant lock static and make it a mutex? and I have an additional question on my own:
Im interested if creating a private static final ReentrantLock lock
is not considered a code smell? I've read that static variables are evil, yet my current use case Im working on looks like ideal place to use one.
Anyone care to help?
Edit with details: I have this class, call it FileProcessor
which does a given job in another thread. My use case is spinning up a few of these instances of this class and doing those jobs. But what I want to do is to make sure only one of them will make this job at once and they will take turns in performing the work.
So I figured out Ill give them a static ReentrantLock
that will lock()
as first thing in run()
block and unlock()
as last. This way they have a shared lock that will guard the processing synchronization.
Thanks.
static
. You can abuse it of course, but you can abuse a lot of things. – Abramabramosynchronized (this) {}
to achieve the same thing without using an explicit lock. This does not provide as much granularity, of course. – Navy