Junit Vs main on "java.util.concurrent.Executors"

1441 단어 자바junit
더 읽 기
Same code with different results.


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();
	}
}



좋은 웹페이지 즐겨찾기