Hi, folks...I have struggled with the concepts of Synchronization for a long time. They are still nebulous...but I could get some insights.
Let us start with two threads invoked on the same object...
Your output should match.
ThreadA: ObjId- Obj1 Entering doStuff
ThreadA: ObjId- Obj1 in doStuff, value of num=10
ThreadB: ObjId- Obj1 Entering doStuff
ThreadB: ObjId- Obj1 in doStuff, value of num=18
ThreadA: ObjId- Obj1 exiting doStuff()
ThreadB: ObjId- Obj1 exiting doStuff()
I use the
word chaos for a non-deterministic output.
1. Two threads invoked on the same object (bim) and two different Runnables (runA & runB) or the same Runnable target
will result in chaos when they enter a non-synchronized method.
2. In order to synchronize, place a synchronized keyword at line 10 after the 'public' keyword. Your output should match.
ThreadA: ObjId- Obj1 Entering doStuff
ThreadA: ObjId- Obj1 in doStuff, value of num=10
ThreadA: ObjId- Obj1 exiting doStuff()
ThreadB: ObjId- Obj1 Entering doStuff
ThreadB: ObjId- Obj1 in doStuff, value of num=18
ThreadB: ObjId- Obj1 exiting doStuff()
Two Threads invoked on different Objects and two different Runnables will cause chaos.
1. Two threads invoked on the different objects (bim1 & bim2) and two different Runnables will produce chaos
even when the method they enter is synchronized because they obtain locks for diferent objects (bim1 & bim2).
ThreadA: ObjId- Obj1 Entering doStuff
ThreadA: ObjId- Obj1 in doStuff, value of num=10
ThreadB: ObjId- Obj2 Entering doStuff
ThreadB: ObjId- Obj2 in doStuff, value of num=18
ThreadA: ObjId- Obj1 exiting doStuff()
ThreadB: ObjId- Obj2 exiting doStuff()
2. But if the Runnable targets are same (Example: use runA for both the threads) they are synchronized, because they have to compete for bim1 lock.
ThreadA: ObjId- Obj1 Entering doStuff
ThreadA: ObjId- Obj1 in doStuff, value of num=10
ThreadA: ObjId- Obj1 exiting doStuff()
ThreadB: ObjId- Obj1 Entering doStuff
ThreadB: ObjId- Obj1 in doStuff, value of num=10
ThreadB: ObjId- Obj1 exiting doStuff()
Simple STATIC synchronization
Output:
threadA:getNextSerialNum()=1001
threadB:getNextSerialNum()=
1002
threadC:getNextSerialNum()=
1002
threadD:getNextSerialNum()=1004
On synchronizing getNextSerialNum(), we have the following required output:
threadA:getNextSerialNum()=1001
threadB:getNextSerialNum()=1002
threadC:getNextSerialNum()=1003
threadD:getNextSerialNum()=1004
Static Block synchronization
while threadA is sleeping at line 10...threadB enters staticB() and prints a message, and blocks waiting to enter the synchronized statement block.
Only when threadA returns form staticA(), will threadB get access to the class-level lock and complete staticB().
Output:
Entering staticA...threadA
In staticA...inside sync block
Entering staticB..threadB //blocks till threadA completes
Exiting staticA...threadA //threadA exits
In staticB...inside sync block //threadB can now get the class lock and enter the synchronized block
Exiting staticB...threadB
I am still a newbie and am still studying synchronized blocks for objects (non-static). Hope this helps a raw beginner.