I found this link on SO:
Code not working when running normally, but working in debug (eclipse)
and on seeing the answers, added a Thread.sleep(0)
inside my while loop and it works.
The question is "why?". Also, is this problem specific to Eclipse
?