Hello All,
I was trying something on yield() method , and found that behavior of the program is not what I expected. I'm sure, I must have mistaken the concept of yield, and now would like to know the science behind this.
As per K&B in page 712 on threads, it says yield() method would keep the locks and would not release them.
From the above statement, I assumed that, when the first
thread enters run, and have a lock on this (MyClass) object, it increments value and would keep the lock with itself without releasing it (due to yield() method). This should prevent the second thread ,entering run and have a lock on this (as already being held with first thread). So I believed that value would be incremented only once and the value would be printed 11 some time later after Main thread is awaken. But this is not what happening
. Value is incremented twice and 12 is printed finally in main method.
Where did I do a mistake in understanding this concept? Please some one clarify me on this.Thanks in advance.