I was expecting that since in the loop sleep(100) is more than wait(1), wait statement would stop waiting and print the Total in the middle of the calculation
Why did the program never outputs?
Waiting for b to complete...
--1--2
Total is: 3
--3--4--5--6--7--8--9
etc
I tried changing the sleep to even 1000 but still the same output
Sandeep Kumar B wrote:
I was expecting that since in the loop sleep(100) is more than wait(1), wait statement would stop waiting and print the Total in the middle of the calculation
but after wait period i.e. 1 miliseconds is over main thread will have to reacquire the lock on ThreadB object b,
so it will block for lock on b to be released, sleep() method call in ThreadB does not cause lock to be released,
so lock of b is still with ThreadB.
Piyush
Pankaj Kumarkk
Ranch Hand
Joined: Apr 17, 2011
Posts: 108
posted
0
I do not know what are you trying to achieve. the current behavior is because sleep blocks but doesn't give up monitor. Thus the other thread doesn't get the monitor and doesn't executes.