线程池的四种基本创建方式
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
public static void main(String[] args) {
//创建固定长度的线程池
ExecutorService pool1 = Executors.newFixedThreadPool(3);
for(int i = 1;i <= 4;i++){
pool1.execute(new MyThread());
}
pool1.shutdown();
//创建可变长度的线程池
ExecutorService pool2 = Executors.newCachedThreadPool();
for(int i = 1;i <= 4;i++){
pool2.execute(new MyThread());
}
//创建单个线程的线程池
ExecutorService pool3 = Executors.newSingleThreadExecutor();
for(int i = 1;i <= 4;i++){
pool3.execute(new MyThread());
}
//创建周期性执行某个任务的线程池
ScheduledExecutorService pool4 = Executors.newScheduledThreadPool(1);
pool4.scheduleAtFixedRate(new Demo(), 2, 3, TimeUnit.SECONDS);
}
}
不错还挺详细的