三种线程池:1、单个线程的线程池 2、固定大小线程池 3、可缓冲线程池
package com.thy.spark.com.thy.d20190417;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolDemo {
public static void main(String[] args) {
//单个线程的线程池
//ExecutorService pool = Executors.newSingleThreadExecutor();
//固定大小线程池
//ExecutorService pool = Executors.newFixedThreadPool(5);
//可缓冲线程池(可有多个线程)
ExecutorService pool = Executors.newCachedThreadPool();
for(int i =0;i<100;i++){
pool.execute(new Runnable() {
@Override
public void run() {
//打印当前线程池的名字
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"is over");
}
});
}
System.out.println("all task is submit");
}
}