The above is a screen print from OCP 7 java se book. page 791.
My question is if a new ReentrantLock
object is created in a method every time and locked, how would that stop two threads from running the code block in between lock
and unlock
? Won't the two threads create a ReentrantLock
object each and lock it? I can imagine how this would work if lock
object was a instance variable only instantiated once and never changed. (preferrably final
).
Am I misunderstanding something?
I had already asked this and Did not get a clear answer.