Today also want to write a simple thread of things, the results have just finished writing I was crazy.
As a result, nothing is output, and the main method can be used to run the output. No way, only for years to find their own answers.
As a result, I have an understanding of all aspects.
First take a look at the code
Package com.mendale.test;import Java.util.date;import org.junit.Test; Public classTest1 {@Test Public voidTest () {MyTime Mt=NewMyTime (); Mt.start (); } Public Static voidMain (string[] args) {MyTime Mt=NewMyTime (); Mt.start (); }}classMyTime extends thread{ Public voidRun () {Boolean flag=true; while(flag) {Try{Thread.Sleep ( +); } Catch(interruptedexception e) {e.printstacktrace (); } Date D=NewDate (); System. out. println (d); } }}
If you use unit tests you will find nothing to output, and no error; This time you're testing the main method, you'll
The discovery unexpectedly has the output, unexpectedly has the output!!! This problem also made me struggle for a long time.
Recall:
The method we use is actually an elf thread (that is, the daemon thread: it is guaranteed to run after other threads have finished running and then over). and unit tests I
Surfing the Internet. It's not an elf thread.
(Subsequent source analysis: suspended)
JUnit unit tests do not respond to thread testing