一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java編程中線程池的基本概念和使用

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

2020-01-20 11:35goldensun JAVA教程

這篇文章主要介紹了Java編程中線程池的基本概念和使用,多線程編程是使Java程序實現并發的一個重要手段,需要的朋友可以參考下

1 引入線程池的原因
  由于線程的生命周期中包括創建、就緒、運行、阻塞、銷毀階段,當我們待處理的任務數目較小時,我們可以自己創建幾個線程來處理相應的任務,但當有大量的任務時,由于創建、銷毀線程需要很大的開銷,運用線程池這些問題就大大的緩解了。

2 線程池的使用
  我們只需要運用Executors類給我們提供的靜態方法,就可以創建相應的線程池:

?
1
2
3
4
5
public static ExecutorSevice newSingleThreadExecutor()
 
public static ExecutorSevice newFixedThreadPool()
 
public static ExecutorSevice newCachedThreadPool()

  newSingleThreadExecutor返回以個包含單線程的Executor,將多個任務交給此Exector時,這個線程處理完一個任務后接著處理下一個任務,若該線程出現異常,將會有一個新的線程來替代。

  newFixedThreadPool返回一個包含指定數目線程的線程池,如果任務數量多于線程數目,那么沒有沒有執行的任務必須等待,直到有任務完成為止。

  newCachedThreadPool根據用戶的任務數創建相應的線程來處理,該線程池不會對線程數目加以限制,完全依賴于JVM能創建線程的數量,可能引起內存不足。

  我們只需要將待執行的任務放入run方法中即可,將Runnable接口的實現類交給線程池的execute方法,作為它的一個參數,如下所示:

?
1
2
3
4
5
6
Executor executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable(){
  public void run(){
    //執行的任務 
 }
}

  如果需要給任務傳遞參數,可以通過創建一個Runnable接口的實現類來完成。

3.實例
(1):newSingleThreadExecutor
MyThread.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
publicclassMyThread extends Thread {
  @Override
  publicvoid run() {
    System.out.println(Thread.currentThread().getName() + "正在執行。。。");
  }
}
TestSingleThreadExecutor.java
publicclassTestSingleThreadExecutor {
  publicstaticvoid main(String[] args) {
    //創建一個可重用固定線程數的線程池
    ExecutorService pool = Executors. newSingleThreadExecutor();
    //創建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
    Thread t1 = new MyThread();
    Thread t2 = new MyThread();
    Thread t3 = new MyThread();
    Thread t4 = new MyThread();
    Thread t5 = new MyThread();
    //將線程放入池中進行執行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //關閉線程池
    pool.shutdown();
  }
}

輸出結果

?
1
2
3
4
5
pool-1-thread-1正在執行。。。
pool-1-thread-1正在執行。。。
pool-1-thread-1正在執行。。。
pool-1-thread-1正在執行。。。
pool-1-thread-1正在執行。。。

(2):newFixedThreadPool
TestFixedThreadPool.Java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
publicclass TestFixedThreadPool {
  publicstaticvoid main(String[] args) {
    //創建一個可重用固定線程數的線程池
    ExecutorService pool = Executors.newFixedThreadPool(2);
    //創建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
    Thread t1 = new MyThread();
    Thread t2 = new MyThread();
    Thread t3 = new MyThread();
    Thread t4 = new MyThread();
    Thread t5 = new MyThread();
    //將線程放入池中進行執行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //關閉線程池
    pool.shutdown();
  }
}

輸出結果

?
1
2
3
4
5
pool-1-thread-1正在執行。。。
pool-1-thread-2正在執行。。。
pool-1-thread-1正在執行。。。
pool-1-thread-2正在執行。。。
pool-1-thread-1正在執行。。。

(3): newCachedThreadPool
TestCachedThreadPool.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
publicclass TestCachedThreadPool {
  publicstaticvoid main(String[] args) {
    //創建一個可重用固定線程數的線程池
    ExecutorService pool = Executors.newCachedThreadPool();
    //創建實現了Runnable接口對象,Thread對象當然也實現了Runnable接口
    Thread t1 = new MyThread();
    Thread t2 = new MyThread();
    Thread t3 = new MyThread();
    Thread t4 = new MyThread();
    Thread t5 = new MyThread();
    //將線程放入池中進行執行
    pool.execute(t1);
    pool.execute(t2);
    pool.execute(t3);
    pool.execute(t4);
    pool.execute(t5);
    //關閉線程池
    pool.shutdown();
  }
}

輸出結果:

?
1
2
3
4
5
pool-1-thread-2正在執行。。。
pool-1-thread-4正在執行。。。
pool-1-thread-3正在執行。。。
pool-1-thread-1正在執行。。。
pool-1-thread-5正在執行。。。

(4):newScheduledThreadPool
TestScheduledThreadPoolExecutor.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
publicclass TestScheduledThreadPoolExecutor {
  publicstaticvoid main(String[] args) {
    ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
    exec.scheduleAtFixedRate(new Runnable() {//每隔一段時間就觸發異常
           @Override
           publicvoid run() {
              //throw new RuntimeException();
              System.out.println("================");
           }
         }, 1000, 5000, TimeUnit.MILLISECONDS);
    exec.scheduleAtFixedRate(new Runnable() {//每隔一段時間打印系統時間,證明兩者是互不影響的
           @Override
           publicvoid run() {
              System.out.println(System.nanoTime());
           }
         }, 1000, 2000, TimeUnit.MILLISECONDS);
  }
}

輸出結果

?
1
2
3
4
5
6
7
8
================
8384644549516
8386643829034
8388643830710
================
8390643851383
8392643879319
8400643939383

延伸 · 閱讀

精彩推薦
  • JAVA教程Java 連接Access數據庫的兩種方式

    Java 連接Access數據庫的兩種方式

    這篇文章主要介紹了Java 連接Access數據庫的兩種方式,本文著重講解使用JDBC連接操作Access數據庫,需要的朋友可以參考下 ...

    junjie5072019-12-24
  • JAVA教程java泛型學習示例

    java泛型學習示例

    Java泛型(Generics)是JDK5開始引入的一個新特性,允許在定義類和接口的時候使用類型參數(Type Parameter)。下面是學習泛型的示例 ...

    Java教程網2192019-11-21
  • JAVA教程Java實現Linux下雙守護進程

    Java實現Linux下雙守護進程

    這篇文章主要介紹了Java實現Linux下雙守護進程的思路、原理以及具體實現方式,非常的詳細,希望對大家有所幫助 ...

    hebedich3122019-12-03
  • JAVA教程java IO流文件的讀寫具體實例

    java IO流文件的讀寫具體實例

    這篇文章主要介紹了java IO流文件的讀寫具體實例,有需要的朋友可以參考一下 ...

    java教程網4202019-10-25
  • JAVA教程java實現八皇后問題示例分享

    java實現八皇后問題示例分享

    這篇文章主要介紹了java實現八皇后問題示例,八皇后問題,是一個古老而著名的問題,是回溯算法的典型案例。該問題是國際西洋棋棋手馬克斯·貝瑟爾于...

    java教程網1842019-11-14
  • JAVA教程實例講解Java并發編程之閉鎖

    實例講解Java并發編程之閉鎖

    這篇文章主要介紹了實例講解Java并發編程之閉鎖,閉鎖相當于一扇門,在閉鎖到達結束狀態之前,這扇門一直是關閉著的,沒有任何線程可以通過,當到達結束狀...

    junjie2642019-12-16
  • JAVA教程java stringbuffer的用法示例

    java stringbuffer的用法示例

    這篇文章主要介紹了java stringbuffer的用法示例,字符串緩沖區,是一個容器(當返回到的是String時而且長度不確定,數據類型不確定時就可以用StringBuffer)其...

    java教程網1282019-10-31
  • JAVA教程java字符串比較獲取字符串出現次數的示例

    java字符串比較獲取字符串出現次數的示例

    java獲取一個字符串在整個字符串出現的次數,下面寫出我的思路和二個實現方法,大家參考使用吧 ...

    java教程網4842019-10-30
主站蜘蛛池模板: 国产午夜精品不卡视频 | 四虎在线成人免费网站 | 欧美日韩国产精品自在自线 | 亚洲AV无码专区国产精品麻豆 | 成人免费片 | 免费看成人毛片日本久久 | 色哟约 | 国产亚洲人成网站天堂岛 | 操老逼视频| 13日本xxxxxxxxx18| 娇妻与公陈峰姚瑶最新版 | 亚洲黄色免费在线观看 | 好大用力深一点视频 | 日韩在线天堂免费观看 | 消息称老熟妇乱视频一区二区 | 97导航| www.色.con | 日本三级免费网站 | 色哟哟在线观看 | 免费看视频网站 | 色综合网亚洲精品久久 | 亚洲免费在线看 | 美味情缘韩国在线观看视频 | 丰腴尤物贵妇浪荡小说 | 被老外玩爽的中国美女视频 | 99久久综合给久久精品 | 亚洲国产精品久久久久 | 激情乱文 | 关晓彤被调教出奶水 | 国产精品福利在线观看入口 | 日韩黄色影视 | 欧美日韩一区二区三区久久 | 99久久精品国语对白 | 免费看男人使劲躁女人小说 | 国产成人精品视频一区二区不卡 | 精品视频手机在线观看免费 | 亚洲丰满女人ass硕大 | nxgx国产 | 啊啊啊好大好爽视频 | bl双性小说 | 好湿好紧太硬了我太爽了h 好湿好滑好硬好爽好深视频 |