I have a problem with thread not sleeping.
I cannot put all my code here. therefore, to reproduce, here is a basic wait code of 5 seconds.
try { int millisec = 5000; System.out.println(new Date()); System.out.println("We wait " + millisec + " milliseconds"); Thread.sleep(millisec); System.out.println(new Date()); } catch (Exception e) { e.printStackTrace(); }
output:
Thu Aug 22 20:01:42 CEST 2013
Wait 5,000 milliseconds
Thu Aug 22 20:01:47 CEST 2013
Everything is fine.
But when I put this code in the stream, do not sleep. An example with this code:
try { Thread aThread = new Thread(new Runnable() { @Override public void run() { try { int millisec = 5000; System.out.println(new Date()); System.out.println("We wait " + millisec + " milliseconds"); Thread.sleep(millisec); System.out.println(new Date()); } catch (Exception e) { e.printStackTrace(); } } }); aThread.start(); } catch (Exception e) { e.printStackTrace(); }
output:
Thu Aug 22 20:07:30 CEST 2013
Wait 5,000 milliseconds
... and nothing more, the flow stops.
I do not understand why. Any ideas please?
EDIT . I use Eclipse and JUnit for testing.
source share