`

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

阅读更多
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();
	}
}






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics