import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.junit.Test; public class JUnitTestVSMain { static class MyBlockedRunnale implements Runnable{ @Override public void run() { try { for(int i = 1; i <= 5; i++){ System.out.println("Sleeping " + i + "s."); Thread.sleep(1000); } } catch (InterruptedException e) { e.printStackTrace(); } } } static void doSomething(){ ExecutorService cacheService = Executors.newCachedThreadPool(); cacheService.execute(new MyBlockedRunnale()); cacheService.shutdown(); System.out.println("shut down..."); } /** * Result: * ======================================= * Sleeping 1s. * shut down... * Sleeping 2s. * Sleeping 3s. * Sleeping 4s. * Sleeping 5s. */ public static void main(String[] args){ doSomething(); } /** * Result: * ======================================= * shut down... * Sleeping 1s. */ @Test public void testShutdown(){ doSomething(); } }
Junit Vs main on "java.util.concurrent.Executors"
Same code with different results.
猜你喜欢
转载自lixh1986.iteye.com/blog/2399319
今日推荐
周排行