线程池(JDK1.5)

 

public class ThreadPoolTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//ExecutorService threadPool = Executors.newFixedThreadPool(3);//固定线程数(当前设为3)
		//ExecutorService threadPool = Executors.newCachedThreadPool();//自动分配
		ExecutorService threadPool = Executors.newSingleThreadExecutor();//单线程,任何时候都保证有一个线程在运行
		for(int i=1;i<=10;i++){
			final int task = i;
			threadPool.execute(new Runnable(){
				@Override
				public void run() {
					for(int j=1;j<=10;j++){
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for  task of " + task);
					}
				}
			});
		}
		System.out.println("all of 10 tasks have committed! ");
		//threadPool.shutdownNow();运行完强行关闭
		
		Executors.newScheduledThreadPool(3).scheduleAtFixedRate(//有定时启动后,有频率参数
	//	Executors.newScheduledThreadPool(3).schedule(//定时启动
				new Runnable(){
					@Override
				public void run() {
					System.out.println("bombing!");
					
				}},
				6,//6秒后启动
				2,//每隔2秒执行一次
				TimeUnit.SECONDS);//单位是秒
	}

}


 

你可能感兴趣的:(jdk,String,Class)