ExecutorService线程池,默认设置线程池的大小

package com.java.basis.threads;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ExecutorServiceTest extends Thread {

	public void run() {
		// 定义线程池的大小
		ExecutorService exec = Executors.newFixedThreadPool(2);
		for (int i = 0; i < 100; i++) {
			final int ii=i;
			// 定义一个内部类
			Runnable run = new Runnable() {
				public void run() {
					try {
						long time = (long) (Math.random() * 1000);
						System.out.println(ii+"休眠" + time + "ms");
						Thread.sleep(time);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			exec.execute(run);
		}
		exec.shutdown();
	}

	public static void main(String[] args) {

		new ExecutorServiceTest().start();
	}

}

  

posted @ 2016-10-14 10:51  fliay  阅读(1601)  评论(0)    收藏  举报