//创建固定线程数的线程池
Executor threadPoolFixed = Executors.newFixedThreadPool(3);
//创建一个线程的线程池
Executor threadPoolSingle = Executors.newSingleThreadExecutor();
//创建缓存的线程池
ExecutorService executorCache = Executors.newCachedThreadPool();
try{
for (int i = 0; i < 10; i++) {
executorCache.execute(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行");
}
});
}
}finally {
((ExecutorService) executorCache).shutdown();
}