最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java編程中線程池的基本概念和使用

 更新時(shí)間:2015年11月16日 08:54:20   投稿:goldensun  
這篇文章主要介紹了Java編程中線程池的基本概念和使用,多線程編程是使Java程序?qū)崿F(xiàn)并發(fā)的一個(gè)重要手段,需要的朋友可以參考下

1 引入線程池的原因
  由于線程的生命周期中包括創(chuàng)建、就緒、運(yùn)行、阻塞、銷毀階段,當(dāng)我們待處理的任務(wù)數(shù)目較小時(shí),我們可以自己創(chuàng)建幾個(gè)線程來(lái)處理相應(yīng)的任務(wù),但當(dāng)有大量的任務(wù)時(shí),由于創(chuàng)建、銷毀線程需要很大的開銷,運(yùn)用線程池這些問(wèn)題就大大的緩解了。

2 線程池的使用
  我們只需要運(yùn)用Executors類給我們提供的靜態(tài)方法,就可以創(chuàng)建相應(yīng)的線程池:

  public static ExecutorSevice newSingleThreadExecutor()

  public static ExecutorSevice newFixedThreadPool()

  public static ExecutorSevice newCachedThreadPool()

  newSingleThreadExecutor返回以個(gè)包含單線程的Executor,將多個(gè)任務(wù)交給此Exector時(shí),這個(gè)線程處理完一個(gè)任務(wù)后接著處理下一個(gè)任務(wù),若該線程出現(xiàn)異常,將會(huì)有一個(gè)新的線程來(lái)替代。

  newFixedThreadPool返回一個(gè)包含指定數(shù)目線程的線程池,如果任務(wù)數(shù)量多于線程數(shù)目,那么沒(méi)有沒(méi)有執(zhí)行的任務(wù)必須等待,直到有任務(wù)完成為止。

  newCachedThreadPool根據(jù)用戶的任務(wù)數(shù)創(chuàng)建相應(yīng)的線程來(lái)處理,該線程池不會(huì)對(duì)線程數(shù)目加以限制,完全依賴于JVM能創(chuàng)建線程的數(shù)量,可能引起內(nèi)存不足。

  我們只需要將待執(zhí)行的任務(wù)放入run方法中即可,將Runnable接口的實(shí)現(xiàn)類交給線程池的execute方法,作為它的一個(gè)參數(shù),如下所示:

Executor executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable(){
  public void run(){
    //執(zhí)行的任務(wù)  
 }
}

  如果需要給任務(wù)傳遞參數(shù),可以通過(guò)創(chuàng)建一個(gè)Runnable接口的實(shí)現(xiàn)類來(lái)完成。

3.實(shí)例
(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)建一個(gè)可重用固定線程數(shù)的線程池
    ExecutorService pool = Executors. newSingleThreadExecutor();
    //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口
    Thread t1 = new MyThread();
    Thread t2 = new MyThread();
    Thread t3 = new MyThread();
    Thread t4 = new MyThread();
    Thread t5 = new MyThread();
    //將線程放入池中進(jìn)行執(zhí)行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //關(guān)閉線程池
    pool.shutdown();
  }
}

輸出結(jié)果

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)建一個(gè)可重用固定線程數(shù)的線程池
    ExecutorService pool = Executors.newFixedThreadPool(2);
    //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口
    Thread t1 = new MyThread();
    Thread t2 = new MyThread();
    Thread t3 = new MyThread();
    Thread t4 = new MyThread();
    Thread t5 = new MyThread();
    //將線程放入池中進(jìn)行執(zhí)行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //關(guān)閉線程池
    pool.shutdown();
  }
}

輸出結(jié)果

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)建一個(gè)可重用固定線程數(shù)的線程池
    ExecutorService pool = Executors.newCachedThreadPool();
    //創(chuàng)建實(shí)現(xiàn)了Runnable接口對(duì)象,Thread對(duì)象當(dāng)然也實(shí)現(xiàn)了Runnable接口
    Thread t1 = new MyThread();
    Thread t2 = new MyThread();
    Thread t3 = new MyThread();
    Thread t4 = new MyThread();
    Thread t5 = new MyThread();
    //將線程放入池中進(jìn)行執(zhí)行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //關(guān)閉線程池
    pool.shutdown();
  }
}

輸出結(jié)果:

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() {//每隔一段時(shí)間就觸發(fā)異常
           @Override
           publicvoid run() {
              //throw new RuntimeException();
              System.out.println("================");
           }
         }, 1000, 5000, TimeUnit.MILLISECONDS);
    exec.scheduleAtFixedRate(new Runnable() {//每隔一段時(shí)間打印系統(tǒng)時(shí)間,證明兩者是互不影響的
           @Override
           publicvoid run() {
              System.out.println(System.nanoTime());
           }
         }, 1000, 2000, TimeUnit.MILLISECONDS);
  }
}

輸出結(jié)果

================
8384644549516
8386643829034
8388643830710
================
8390643851383
8392643879319
8400643939383

相關(guān)文章

  • java兩個(gè)數(shù)組合并為一個(gè)數(shù)組的幾種方法

    java兩個(gè)數(shù)組合并為一個(gè)數(shù)組的幾種方法

    這篇文章主要給大家介紹了關(guān)于java兩個(gè)數(shù)組合并為一個(gè)數(shù)組的幾種方法,最近在寫代碼時(shí)遇到了需要合并兩個(gè)數(shù)組的需求,文中將每種方法都介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼

    MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼

    這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)多數(shù)據(jù)源的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • java單例五種實(shí)現(xiàn)模式解析

    java單例五種實(shí)現(xiàn)模式解析

    這篇文章主要介紹了java單例五種實(shí)現(xiàn)模式解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Java隨機(jī)數(shù)算法原理與實(shí)現(xiàn)方法實(shí)例詳解

    Java隨機(jī)數(shù)算法原理與實(shí)現(xiàn)方法實(shí)例詳解

    這篇文章主要介紹了Java隨機(jī)數(shù)算法原理與實(shí)現(xiàn)方法,簡(jiǎn)單分析了隨機(jī)數(shù)算法的原理并結(jié)合具體實(shí)例形式給出了java編程計(jì)算隨機(jī)數(shù)的具體操作技巧,需要的朋友可以參考下
    2017-09-09
  • Java中Objects.equals踩坑記錄

    Java中Objects.equals踩坑記錄

    最近在工作中發(fā)現(xiàn)一個(gè)問(wèn)題,覺(jué)著還是挺有必要記錄下的,這篇文章主要給大家介紹了關(guān)于Java中Objects.equals踩坑的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • java實(shí)現(xiàn)簡(jiǎn)易的學(xué)籍管理系統(tǒng)

    java實(shí)現(xiàn)簡(jiǎn)易的學(xué)籍管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易的學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot解決mysql連接8小時(shí)問(wèn)題

    SpringBoot解決mysql連接8小時(shí)問(wèn)題

    服務(wù)連接mysql數(shù)據(jù)庫(kù),8小時(shí)沒(méi)有數(shù)據(jù)庫(kù)的操作時(shí)候,數(shù)據(jù)庫(kù)會(huì)主動(dòng)斷開連接釋放資源,本文就詳細(xì)的介紹一下解決方法,感興趣的可以了解一下
    2023-08-08
  • Mybatis之Mapper動(dòng)態(tài)代理實(shí)例解析

    Mybatis之Mapper動(dòng)態(tài)代理實(shí)例解析

    這篇文章主要介紹了Mybatis之Mapper動(dòng)態(tài)代理實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Springboot實(shí)現(xiàn)過(guò)濾器的兩種方式

    Springboot實(shí)現(xiàn)過(guò)濾器的兩種方式

    今天通過(guò)本文給大家分享Springboot實(shí)現(xiàn)過(guò)濾器的兩種方式,第一種是spring容器注冊(cè)filter,第二種方式是通過(guò)@WebFilter 注解來(lái)配置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2023-10-10
  • Java中的定時(shí)器Timer詳解

    Java中的定時(shí)器Timer詳解

    這篇文章主要為大家詳細(xì)介紹了Java定時(shí)器Timer的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評(píng)論

上栗县| 镇雄县| 武强县| 新晃| 吉林市| 兴国县| 弥勒县| 府谷县| 河东区| 安多县| 怀化市| 阜阳市| 高尔夫| 隆昌县| 浦江县| 西乌| 清涧县| 荆门市| 临城县| 安乡县| 扎兰屯市| 鄂温| 汝州市| 东阳市| 米林县| 洪泽县| 贞丰县| 南川市| 滦南县| 宿松县| 都江堰市| 秭归县| 仲巴县| 鹰潭市| 涟源市| 高陵县| 鄂温| 邯郸县| 凌源市| 泰和县| 贵溪市|