Java: Thread.sleep () in thread: no wait

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.

+4
source share
2 answers

Edit:

The problem turned out to be how JUnit works. He does not know that background threads were generated, so when the test thread completes, it kills the rest of the threads without waiting for them.


Your code correctly spits out both date strings when I try to use it in a small main() class. Below are some possibilities why you do not see the output in your application:

  • The thread creating aThread may be the daemon itself. So aThread is a daemon because its daemon status is derived from spawning thread status. If the JVM ends before the 2nd System.out.println(...) , aThread will be killed. If there is a question about this, you should do:

     Thread aThread = new Thread(new Runnable() { ... // ensure the deamon flag is off _before_ we start the thread aThread.setDaemon(false); aThread.start(); 
  • Another possibility is that something really closes System.out until the 2nd println(...); . Hardly, but possible. Do you use finishing hooks for cleaning?

  • A third possibility is that the output is actually printed, but your IDE or your console somehow does not display the output.

  • Another possibility is that System.out.println(...); chose an IOException . It may be interesting to check the value of System.out.checkError() to see if it is true , although I'm not sure how you will display it.

Try creating a temporary file instead of printing. Sort of:

 new File("/var/tmp/" + System.currentTimeMillis()).createNewFile(); 

Then you should see 2 files in "/var/tmp" (or wherever the temp directory on your OS is located).

You should also try changing System.out to System.err to see that it changes something. Doubtful, but worth a try.

If they do not work, then something kills the JVM a lot, so it cannot wait for background threads.

+5
source

Well, I think you did not call join () on your main thread (if you use JUnit, then the thread that calls the test case and creates the thread object).

+1
source

Source: https://habr.com/ru/post/1498459/


All Articles