各种线程池实现,pool
列举几种线程池实现:
【1】ExecutorService
http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/util/concurrent/ExecutorService.html
【2】ScheduledExecutorService
http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/util/concurrent/ScheduledExecutorService.html
【3】ThreadPoolExecutor
http://www.gznc.edu.cn/yxsz/jjglxy/book/Java_api/java/util/concurrent/ThreadPoolExecutor.html
【4】ScheduledThreadPoolExecutor
public static void main(String[] args) {
// 创建一个可重用固定线程集合的线程池,以共享的无界队列(a shared unbounded queue)方式来运行这些线程
ExecutorService pool1 = Executors.newFixedThreadPool(3);
// 创建一个可根据需要创建新线程的线程池,但是在以前构造的线程可用时将重用它们
ExecutorService pool2 = Executors.newCachedThreadPool();
// 创建一个使用单个worker线程的Executor,以无界队列方式来运行该线程
ExecutorService pool3 = Executors.newSingleThreadExecutor();
// 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行
ScheduledExecutorService pool4 = Executors.newScheduledThreadPool(3);
// 创建一个单线程执行程序,它可安排在给定延迟后运行命令或者定期地执行
ScheduledExecutorService pool5 = Executors.newSingleThreadScheduledExecutor();
// 创建一个可伸缩线程池
ThreadPoolExecutor pool6 = new ThreadPoolExecutor(3, 5, 3,
TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(3), new ThreadPoolExecutor.DiscardOldestPolicy());
// 创建一个可伸缩线程池,它可安排在给定延迟后运行命令或者定期地执行
ScheduledThreadPoolExecutor pool7 = new ScheduledThreadPoolExecutor(5);
}