Hi all, this is from JQ+ Mock Question ID: 988384270281
The answer given is: Here, there are two threads. But when one
thread enters run(), it accquires lock on obj and enters into infinite loop. Now, the second thread also enters run but has to wait as it is also trying to accquire the same lock (obj is a static field). So, in affect, only the thread that gets the lock first, keeps on running. The second thread never gets a chance. so the values printed are sequencial, x and y are always printed as equal and they get incremented by 1 on each line.
However, when I tried removing the "static" on line 04, I could see that both threads are shown in the println on line 13. why is it that "x" and "y" are still incrementing by 1 on each line (which suggests to me that syncronization still works) but both threads are shown eventhough we have an infinite loop (which suggests to me that syncronization doesn't seem to work). The screen output I got is:
instead of:
[ August 07, 2006: Message edited by: Shanel Jacob ]