1. This code doesnt work as expected, it only prints Y's. and it looks like the other thread is started but didnt change the value of "name". using "obj" as lock in the above code works well. 2. Also using "this" as lock doesnt work(didnt synchronize).. Any clues why? Thanks
Does this behavior is because of the name variable is static? I think when the threads started running that time the value of name is "Y". Would you like to remove the static modifier from the variable and check the output again... [ August 24, 2007: Message edited by: Al Mamun ]
I marked name as non-static and synchronized on 'this'. Now I get 200 X's followed by 200 Y's. My question is:
Can we be sure to get 200 Z's followed by 200 Y's...? I think the output is not predictable...please help me understand this.
Since the two threads are working on different instances, they can both acquire the "this" lock for their respective objects. I think we can get any output:? ZZZZZ...YYYYY... YYYYY....ZZZZZ... ZYZYZY....