java - Synchronising twice on the same object? -
i wondering if in java odd behaviour if synchronise twice on same object?
the scenario follows
pulbic class sillyclassname { object moo; ... public void method1(){ synchronized(moo) { .... method2(); .... } } public void method2(){ synchronized(moo) { dostuff(); } } }
both methods use object , synchronised on it. second method when called first method stop because it's locked?
i don't think because it's same thread i'm unsure of other odd results might occur.
reentrant
synchronized blocks use reentrant locks, means if thread holds lock, can re-aquire without problems. therefore code work expect.
see bottom of java tutorial page intrinsic locks , synchronization.
to quote of 2015-01…
reentrant synchronization
recall thread cannot acquire lock owned thread. thread can acquire lock owns. allowing thread acquire same lock more once enables reentrant synchronization. describes situation synchronized code, directly or indirectly, invokes method contains synchronized code, , both sets of code use same lock. without reentrant synchronization, synchronized code have take many additional precautions avoid having thread cause block.
Comments
Post a Comment