I\'m really confused by this: some of my code is not working when i run my program normally in eclipse, but it does wok when i run through each step separately using the deb
Using Thread.sleep()
, as the other answers have suggested, should solve the problem but it is not a very good approach. Instead, we should be using Thread.yield()
.
Why yield
and not sleep
?
Refer: Difference between Thread.Sleep(0) and Thread.Yield() and Are Thread.sleep(0) and Thread.yield() statements equivalent?
Why this works?
When we just run the threads, the OS puts them to "idle" state and when it is expected to "wake-up", it does not. On the other hand, in debug mode, we have a controlled environment. The OS has little control over it as everything goes on step-by-step, slowly. If we run the debug a few times without any break-points, after a few successful runs, we should see the same effect.