CachedThreadPool构造方法
public static ExecutorService newCachedThreadPooll() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}
复制代码
该线程池特点:无核心线程数,只有非核心线程,非核心线程数可达到Integer.MAX_VALUE,且非核心线程等待时间为60秒,采用SynchronousQueue队列。
使用案例:
val pool: ExecutorService = Executors.newCachedThreadPool()
复制代码
txt.click {
for (i in 0 until 30) {
val runnable = Runnable {
try {
Thread.sleep(2000)
log("当前线程是:", Thread.currentThread.name)
}catch(e: Exception) {
e.printStackTrace()
}
}
pool.execute(runnable)
}
}
复制代码