Hi ranchers,
Jim Yingst posted November 05, 2006 11:25 AM
Well, the thing is that the run() method is synchronized, so yield() will have no effect since the other thread can't acquire the lock it would need in order to run(). Likewise sleep() doesn't give up the lock, (...)
True, but we wanted the sleep or yield method called not in an endless loop but in a short loop.
OK, both have no effect in a synchronized method, but it is usefull when you're
testing the effect with / without synchronize.
As without yield or sleep the short loop
for(int i= 0 ;i<100 ;i++) { ... in Rohit's code, the loop will so fast be over, that the other thread never interrupts the first, synchronized or not.
So you won't see a difference in the output with or without synchronization.
The sleep or yield is only for this test.
Yours,
Bu.