`

java需要关注的知识点---并发之使用Executor

 
阅读更多
public class CachedthreadPool {
	public static void main(String[] args) {
		ExecutorService es = Executors.newCachedThreadPool();
		for ( int i = 0; i< 5; i++)
			es.execute(new LiftOff());
		es.shutdown();
	}
}


public class MoreBasicThread {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for (int i = 0; i< 5; i++) {
			new Thread(new LiftOff()).start();
		}
		System.out.println("Waiting for liftOff");
	}

}



public class SingleThreadExecutor {
public static void main(String[] args) {
	// 保证无论何时都只有一个线程在运行。
	ExecutorService es = Executors.newSingleThreadExecutor();
	for(int i = 0; i < 5; i++) {
		es.execute(new LiftOff());
	}
	es.shutdown();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics