2019-03-12 15:05:10 1900瀏覽
今天扣丁學(xué)堂Java培訓(xùn)老師給大家介紹一下關(guān)于Java四種常用線程池的詳細(xì)介紹,希望對同學(xué)們學(xué)習(xí)Java開發(fā)有所幫助,下面我們一起來看一下吧。
package com.study.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolExecutorTest { public static void main(String[] args) { //創(chuàng)建一個可緩存線程池 ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { try { //sleep可明顯看到使用的是線程池里面以前的線程,沒有創(chuàng)建新的線程 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } cachedThreadPool.execute(new Runnable() { public void run() { //打印正在執(zhí)行的緩存線程信息 System.out.println(Thread.currentThread().getName()+"正在被執(zhí)行"); } }); } } }
pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行
package com.study.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolExecutorTest { public static void main(String[] args) { //創(chuàng)建一個可重用固定個數(shù)的線程池 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); for (int i = 0; i < 10; i++) { fixedThreadPool.execute(new Runnable() { public void run() { try { //打印正在執(zhí)行的緩存線程信息 System.out.println(Thread.currentThread().getName()+"正在被執(zhí)行"); Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
pool-1-thread-1正在被執(zhí)行 pool-1-thread-2正在被執(zhí)行 pool-1-thread-3正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-2正在被執(zhí)行 pool-1-thread-3正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行 pool-1-thread-2正在被執(zhí)行 pool-1-thread-3正在被執(zhí)行 pool-1-thread-1正在被執(zhí)行
package com.study.test; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ThreadPoolExecutorTest { public static void main(String[] args) { //創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行——延遲執(zhí)行 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); //延遲1秒執(zhí)行 scheduledThreadPool.schedule(new Runnable() { public void run() { System.out.println("延遲1秒執(zhí)行"); } }, 1, TimeUnit.SECONDS); } }
延遲1秒執(zhí)行
package com.study.test; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class ThreadPoolExecutorTest { public static void main(String[] args) { //創(chuàng)建一個定長線程池,支持定時及周期性任務(wù)執(zhí)行——定期執(zhí)行 ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); //延遲1秒后每3秒執(zhí)行一次 scheduledThreadPool.scheduleAtFixedRate(new Runnable() { public void run() { System.out.println("延遲1秒后每3秒執(zhí)行一次"); } }, 1, 3, TimeUnit.SECONDS); } }
延遲1秒后每3秒執(zhí)行一次 延遲1秒后每3秒執(zhí)行一次 .............
package com.study.test; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class TestThreadPoolExecutor { public static void main(String[] args) { //創(chuàng)建一個單線程化的線程池 ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor(); for (int i = 0; i < 10; i++) { final int index = i; singleThreadExecutor.execute(new Runnable() { public void run() { try { //結(jié)果依次輸出,相當(dāng)于順序執(zhí)行各個任務(wù) System.out.println(Thread.currentThread().getName()+"正在被執(zhí)行,打印的值是:"+index); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); } } }
pool-1-thread-1正在被執(zhí)行,打印的值是:0 pool-1-thread-1正在被執(zhí)行,打印的值是:1 pool-1-thread-1正在被執(zhí)行,打印的值是:2 pool-1-thread-1正在被執(zhí)行,打印的值是:3 pool-1-thread-1正在被執(zhí)行,打印的值是:4 pool-1-thread-1正在被執(zhí)行,打印的值是:5 pool-1-thread-1正在被執(zhí)行,打印的值是:6 pool-1-thread-1正在被執(zhí)行,打印的值是:7 pool-1-thread-1正在被執(zhí)行,打印的值是:8 pool-1-thread-1正在被執(zhí)行,打印的值是:9
package com.study.test; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; class TempThread implements Runnable { @Override public void run() { // 打印正在執(zhí)行的緩存線程信息 System.out.println(Thread.currentThread().getName() + "正在被執(zhí)行"); try { // sleep一秒保證3個任務(wù)在分別在3個線程上執(zhí)行 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } public class TestThreadPoolExecutor { public static void main(String[] args) { // 創(chuàng)建數(shù)組型緩沖等待隊列 BlockingQueue<Runnable> bq = new ArrayBlockingQueue<Runnable>(10); // ThreadPoolExecutor:創(chuàng)建自定義線程池,池中保存的線程數(shù)為3,允許最大的線程數(shù)為6 ThreadPoolExecutor tpe = new ThreadPoolExecutor(3, 6, 50, TimeUnit.MILLISECONDS, bq); // 創(chuàng)建3個任務(wù) Runnable t1 = new TempThread(); Runnable t2 = new TempThread(); Runnable t3 = new TempThread(); // Runnable t4 = new TempThread(); // Runnable t5 = new TempThread(); // Runnable t6 = new TempThread(); // 3個任務(wù)在分別在3個線程上執(zhí)行 tpe.execute(t1); tpe.execute(t2); tpe.execute(t3); // tpe.execute(t4); // tpe.execute(t5); // tpe.execute(t6); // 關(guān)閉自定義線程池 tpe.shutdown(); } }
pool-1-thread-1正在被執(zhí)行 pool-1-thread-2正在被執(zhí)行 pool-1-thread-3正在被執(zhí)行
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>