Java編程中線程池的基本概念和使用
1 引入線程池的原因
由于線程的生命周期中包括創(chuàng)建、就緒、運行、阻塞、銷毀階段,當我們待處理的任務數(shù)目較小時,我們可以自己創(chuàng)建幾個線程來處理相應的任務,但當有大量的任務時,由于創(chuàng)建、銷毀線程需要很大的開銷,運用線程池這些問題就大大的緩解了。
2 線程池的使用
我們只需要運用Executors類給我們提供的靜態(tài)方法,就可以創(chuàng)建相應的線程池:
public static ExecutorSevice newSingleThreadExecutor() public static ExecutorSevice newFixedThreadPool() public static ExecutorSevice newCachedThreadPool()
newSingleThreadExecutor返回以個包含單線程的Executor,將多個任務交給此Exector時,這個線程處理完一個任務后接著處理下一個任務,若該線程出現(xiàn)異常,將會有一個新的線程來替代。
newFixedThreadPool返回一個包含指定數(shù)目線程的線程池,如果任務數(shù)量多于線程數(shù)目,那么沒有沒有執(zhí)行的任務必須等待,直到有任務完成為止。
newCachedThreadPool根據(jù)用戶的任務數(shù)創(chuàng)建相應的線程來處理,該線程池不會對線程數(shù)目加以限制,完全依賴于JVM能創(chuàng)建線程的數(shù)量,可能引起內(nèi)存不足。
我們只需要將待執(zhí)行的任務放入run方法中即可,將Runnable接口的實現(xiàn)類交給線程池的execute方法,作為它的一個參數(shù),如下所示:
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable(){
public void run(){
//執(zhí)行的任務
}
}
如果需要給任務傳遞參數(shù),可以通過創(chuàng)建一個Runnable接口的實現(xiàn)類來完成。
3.實例
(1):newSingleThreadExecutor
MyThread.java
publicclassMyThread extends Thread {
@Override
publicvoid run() {
System.out.println(Thread.currentThread().getName() + "正在執(zhí)行。。。");
}
}
TestSingleThreadExecutor.java
publicclassTestSingleThreadExecutor {
publicstaticvoid main(String[] args) {
//創(chuàng)建一個可重用固定線程數(shù)的線程池
ExecutorService pool = Executors. newSingleThreadExecutor();
//創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當然也實現(xiàn)了Runnable接口
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
//將線程放入池中進行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//關閉線程池
pool.shutdown();
}
}
輸出結果
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í)行。。。
(2):newFixedThreadPool
TestFixedThreadPool.Java
publicclass TestFixedThreadPool {
publicstaticvoid main(String[] args) {
//創(chuàng)建一個可重用固定線程數(shù)的線程池
ExecutorService pool = Executors.newFixedThreadPool(2);
//創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當然也實現(xiàn)了Runnable接口
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
//將線程放入池中進行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//關閉線程池
pool.shutdown();
}
}
輸出結果
pool-1-thread-1正在執(zhí)行。。。 pool-1-thread-2正在執(zhí)行。。。 pool-1-thread-1正在執(zhí)行。。。 pool-1-thread-2正在執(zhí)行。。。 pool-1-thread-1正在執(zhí)行。。。
(3): newCachedThreadPool
TestCachedThreadPool.java
publicclass TestCachedThreadPool {
publicstaticvoid main(String[] args) {
//創(chuàng)建一個可重用固定線程數(shù)的線程池
ExecutorService pool = Executors.newCachedThreadPool();
//創(chuàng)建實現(xiàn)了Runnable接口對象,Thread對象當然也實現(xiàn)了Runnable接口
Thread t1 = new MyThread();
Thread t2 = new MyThread();
Thread t3 = new MyThread();
Thread t4 = new MyThread();
Thread t5 = new MyThread();
//將線程放入池中進行執(zhí)行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//關閉線程池
pool.shutdown();
}
}
輸出結果:
pool-1-thread-2正在執(zhí)行。。。 pool-1-thread-4正在執(zhí)行。。。 pool-1-thread-3正在執(zhí)行。。。 pool-1-thread-1正在執(zhí)行。。。 pool-1-thread-5正在執(zhí)行。。。
(4):newScheduledThreadPool
TestScheduledThreadPoolExecutor.java
publicclass TestScheduledThreadPoolExecutor {
publicstaticvoid main(String[] args) {
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
exec.scheduleAtFixedRate(new Runnable() {//每隔一段時間就觸發(fā)異常
@Override
publicvoid run() {
//throw new RuntimeException();
System.out.println("================");
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
exec.scheduleAtFixedRate(new Runnable() {//每隔一段時間打印系統(tǒng)時間,證明兩者是互不影響的
@Override
publicvoid run() {
System.out.println(System.nanoTime());
}
}, 1000, 2000, TimeUnit.MILLISECONDS);
}
}
輸出結果
================ 8384644549516 8386643829034 8388643830710 ================ 8390643851383 8392643879319 8400643939383
相關文章
java兩個數(shù)組合并為一個數(shù)組的幾種方法
這篇文章主要給大家介紹了關于java兩個數(shù)組合并為一個數(shù)組的幾種方法,最近在寫代碼時遇到了需要合并兩個數(shù)組的需求,文中將每種方法都介紹的非常詳細,需要的朋友可以參考下2023-07-07
MyBatis-Plus實現(xiàn)多數(shù)據(jù)源的示例代碼
這篇文章主要介紹了MyBatis-Plus實現(xiàn)多數(shù)據(jù)源的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Java隨機數(shù)算法原理與實現(xiàn)方法實例詳解
這篇文章主要介紹了Java隨機數(shù)算法原理與實現(xiàn)方法,簡單分析了隨機數(shù)算法的原理并結合具體實例形式給出了java編程計算隨機數(shù)的具體操作技巧,需要的朋友可以參考下2017-09-09

