Junit Vs main on "java.util.concurrent.Executors"_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Junit Vs main on "java.util.concurrent.Executors"

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

 2017/11/10 19:03:24  Lixh1986  程序员俱乐部  我要评论(0)
  • 摘要:Samecodewithdifferentresults.importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importorg.junit.Test;publicclassJUnitTestVSMain{staticclassMyBlockedRunnaleimplementsRunnable{@Overridepublicvoidrun(){try{for(inti=1;i<
  • 标签:Java
Same code with different results.

class="java">

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






发表评论
用户名: 匿名