通过newCachedThreadPool创建线程代码
2017-10-19 21:05
204 查看
和newFixedThreadPool创建线程代码类似,不同的是通过newCachedThreadPool创建线程代码活跃的线程数目不固定,可以任务多少根据需要创建线程。不必担心关闭问题,因为会自动关闭60s不活动的线程。代码如下:
输出的结果为:
可见有10个线程在执行创建的10个任务,这个是跟newFixedThreadPool不同的,newFixedThreadPool只能创建指定数目的线程。
public class CachedThreadPoolRunner implements Runnable { private Integer integer; private long time; public CachedThreadPoolRunner(Integer integer, long time){ this.integer = integer; this.time = time; } public void run() { try { System.out.println(Thread.currentThread().getName() + "执行第" + integer + "个任务"); Thread.sleep(time); System.out.println(Thread.currentThread().getName() + "执行第" + integer + "个任务结束"); } catch (InterruptedException e) { e.printStackTrace(); } } }
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class CachedThreadPoolTest { public static void main(String[] args) { ExecutorService executorService = Executors.newCachedThreadPool(); long time = 500; for (int i = 1; i <= 10 ; i++) { CachedThreadPoolRunner runner = new CachedThreadPoolRunner(i, time); System.out.println("准备创建第" + i + "个任务"); executorService.submit(runner); } try { Thread.sleep(time); executorService.shutdown(); executorService.awaitTermination(time, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } } }
输出的结果为:
准备创建第1个任务 准备创建第2个任务 准备创建第3个任务 pool-1-thread-1执行第1个任务 准备创建第4个任务 pool-1-thread-2执行第2个任务 准备创建第5个任务 pool-1-thread-4执行第4个任务 准备创建第6个任务 pool-1-thread-3执行第3个任务 准备创建第7个任务 pool-1-thread-5执行第5个任务 准备创建第8个任务 pool-1-thread-7执行第7个任务 pool-1-thread-6执行第6个任务 准备创建第9个任务 pool-1-thread-8执行第8个任务 准备创建第10个任务 pool-1-thread-9执行第9个任务 pool-1-thread-10执行第10个任务 pool-1-thread-1执行第1个任务结束 pool-1-thread-2执行第2个任务结束 pool-1-thread-5执行第5个任务结束 pool-1-thread-4执行第4个任务结束 pool-1-thread-9执行第9个任务结束 pool-1-thread-8执行第8个任务结束 pool-1-thread-3执行第3个任务结束 pool-1-thread-7执行第7个任务结束 pool-1-thread-6执行第6个任务结束 pool-1-thread-1 4000 0执行第10个任务结束
可见有10个线程在执行创建的10个任务,这个是跟newFixedThreadPool不同的,newFixedThreadPool只能创建指定数目的线程。
相关文章推荐
- 通过newFixedThreadPool创建线程代码
- java 通过Executors.newFixedThreadPool创建固定大小的线程执行器
- 关于Executors.newFixedThreadPool何时创建新线程
- Java newCachedThreadPool 创建线程池
- 四种线程池创建及使用(一)newScheduledThreadPool和newCachedThreadPool的使用方法详解
- Android创建全局线程池类之newCachedThreadPool
- JAVA通过继承Thread来创建线程
- 线程池的使用(newCachedThreadPool)
- Java中OutOfMemoryError与unable to create new native thread(JVM创建大量线程)的关系
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池
- 使用无限大小线程池 newCachedThreadPool 可能遇到的问题
- newCachedThreadPool线程池
- java中通过Thread类来创建线程
- java.lang.OutOfMemoryError: unable to create new native thread无法创建本地线程分析
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
- JAVA通过继承Thread来创建线程
- Java中OutOfMemoryError与unable to create new native thread(JVM创建大量线程)的关系
- ExecutorService常用方法和newFixedThreadPool创建固定大小的线程池