FixedThreadPool構造方法
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}
複製程式碼
該執行緒池特點:int引數即為核心執行緒數,只有核心執行緒,無非核心執行緒,阻塞佇列無界(可達系統能承受的最大值)。
使用案例:
val pool: ExecutorService = Executors.newFixedThreadPool(5)
複製程式碼
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)
}
}
複製程式碼