java - Why can't wake up this thread -
i want test thread.sleep() method, , found interesting thing.. when invoke main() method , console print "usera sleeping..." , "usera waking...", means program awakened , when use junit method run same code main() method, won't print "usera waking..." ... appreciate can explain .
package com.lmh.threadlocal; import org.junit.test; public class threadtest { public static void main(string [] args) { new thread(new usera()).start(); } @test public void testwakeup(){ new thread(new usera()).start(); } } class usera implements runnable{ @override public void run() { try { system.out.println("usera sleeping..."); thread.sleep(1000); system.out.println("usera waking..."); } catch (interruptedexception e) { e.printstacktrace(); } } }
my guess junit tearing down test before sleep finishes, because test execution thread exits test method before sleep finishes. try
@test public void testwakeup() throws exception { thread t = new thread(new usera()); t.start(); t.join(); }
Comments
Post a Comment