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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - 理解java多線程中ExecutorService使用

理解java多線程中ExecutorService使用

2020-07-09 10:40stefan.king JAVA教程

這篇文章主要幫助大家理解java多線程中ExcetorServiced的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

java.util.concurrent包里提供了關(guān)于多線程操作的類,平常用的比較多的是ExecutorService及其實(shí)現(xiàn)類(如ThreadPoolExecutor等),Executor,Executors,F(xiàn)uture,Callable等

1. ExecutorService(繼承自Executor)接口:提供了一些異步的多線程操作方法,如execute(), submit(), shutdown(), shutdownNow()等

2. Executor接口:執(zhí)行提交的任務(wù)(線程),只有一個方法 execute(Runnable a)

2. Executors類: 提供了一些工廠方法和一些公共方法來操作Executor子類和ThreadFactory等,如newXXX(),xxxThreadFactory()等

3. Futrue接口:代表了線程執(zhí)行結(jié)果,提供了獲取線程執(zhí)行結(jié)果和取消線程的方法,如get(),cancle()等

4. Callable接口:JDK1.5提供的有返回值的線程執(zhí)行新接口

對ExecutorService和Future的理解做簡單記錄

代碼:

?
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Main {
  private static int count = 0;
 
  public static void main(String[] args){
    List<Future> resultList = new LinkedList<>();
 
    /**
     * Executors.newCachedThreadPool() 創(chuàng)建一個線程緩存池,若60s中線程沒有被使用,則會停止線程并從緩存池中移除
     * Executors.newScheduledThreadPool() 創(chuàng)建一個固定容量的線程池,里邊的線程按照設(shè)定的調(diào)度時間執(zhí)行
     * Executors.newFixedThreadPool()  擁有固定容量的線程緩存池
     * Executors.newSingleThreadExecutor() 容量為一的線程緩存池,只會有一個線程
     */
    ExecutorService executorService = Executors.newCachedThreadPool();
    for(int i=0; i<10; i++){
      Future future = executorService.submit(new Callable<String>() {
        @Override
        public String call() {
          try {
            System.out.println(Thread.currentThread().getName());
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          int count = Main.count;
          System.out.println(Thread.currentThread().getName() + "..start Main count:..." + count);
          Main.count = ++count;
          System.out.println(Thread.currentThread().getName() + "..end Main count:..." + Main.count);
          return Thread.currentThread().getName();
        }
      });
      resultList.add(future);
    }
    executorService.shutdown();
    for(Future future: resultList){
      try {
        System.out.println(future.get() + "..is over...");
      } catch (InterruptedException e) {
        e.printStackTrace();
      } catch (ExecutionException e) {
        e.printStackTrace();
      }
    }
    System.out.println("main thread end...");
  }
}

輸出:

?
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
pool-1-thread-1
pool-1-thread-2
pool-1-thread-3
pool-1-thread-4
pool-1-thread-5
pool-1-thread-6
pool-1-thread-7
pool-1-thread-8
pool-1-thread-9
pool-1-thread-10
pool-1-thread-1..start Main count:...0
pool-1-thread-2..start Main count:...0
pool-1-thread-3..start Main count:...1
pool-1-thread-2..end Main count:...1
pool-1-thread-1..end Main count:...1
pool-1-thread-3..end Main count:...2
pool-1-thread-1..is over...
pool-1-thread-2..is over...
pool-1-thread-4..start Main count:...2
pool-1-thread-3..is over...
pool-1-thread-4..end Main count:...3
pool-1-thread-4..is over...
pool-1-thread-5..start Main count:...3
pool-1-thread-5..end Main count:...4
pool-1-thread-5..is over...
pool-1-thread-6..start Main count:...4
pool-1-thread-6..end Main count:...5
pool-1-thread-6..is over...
pool-1-thread-7..start Main count:...5
pool-1-thread-7..end Main count:...6
pool-1-thread-7..is over...
pool-1-thread-8..start Main count:...6
pool-1-thread-8..end Main count:...7
pool-1-thread-8..is over...
pool-1-thread-9..start Main count:...7
pool-1-thread-9..end Main count:...8
pool-1-thread-9..is over...
pool-1-thread-10..start Main count:...8
pool-1-thread-10..end Main count:...9
pool-1-thread-10..is over...
main thread end... //主線程在所有線程執(zhí)行完成后結(jié)束

控制臺在等待5秒后打印出上邊的輸出結(jié)果,原因是所有的線程啟動的時候是一個并發(fā)操作,都會去等待5秒,所以整體看來只等了5秒,這是一個并發(fā)操作

總結(jié):

1. ExecutorService提供的execute()方法和submit()方法的區(qū)別:

  a. execute()方法只接受Runnable類型的實(shí)例,所以不能拿到返回值,也不能動態(tài)獲取線程執(zhí)行的情況

  b. submit()方法接受Runnable和Callable實(shí)例,會返回Future實(shí)例,F(xiàn)uture實(shí)例的get()方法可以獲取線程執(zhí)行返回值,并能拋出線程執(zhí)行異常。所以如果要獲取線程執(zhí)行返回的結(jié)果,并能處理線程執(zhí)行時可能出現(xiàn)的異常,或者想中途取消線程執(zhí)行時可以使用submit()方法

2. 通過輸出可以看到main方法(主線程)在所有線程執(zhí)行完成后結(jié)束,原因:

  a. 通過submit()方法獲取Future實(shí)例,并通過Future實(shí)例的get()方法獲取線程返回結(jié)果,而Future實(shí)例的get()方法會等待線程執(zhí)行完畢才會返回,所以main方法會等待所有子線程結(jié)束才會結(jié)束

  b. 若去掉上邊紅色標(biāo)注的for循環(huán),則main方法(主線程)會提前結(jié)束,而不會等待所有子線程結(jié)束

補(bǔ)充:

1. 多個線程并發(fā)執(zhí)行時,若其中某一個線程出現(xiàn)了異常并且沒有被處理,則該線程會自動停止執(zhí)行,但其他線程還是會正常執(zhí)行,這就是為什么tomcat請求出現(xiàn)異常時,tomcat還可以繼續(xù)提供服務(wù)的原因。

2. tomcat提供了線程池和等待池,每一個請求過來都會重新啟動一個新的線程處理該請求,若線程池中線程用完,再來請求的時候就會放到等待池中等待,當(dāng)其中有線程釋放回線程池中時,就會為等待池中的請求分配線程處理請求。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99精品网站 | 香蕉大久久 | 欧美a级完整在线观看 | 亚洲欧美优优色在线影院 | 日产国产精品亚洲系列 | 2022天天干 | 欧美成年黄网站色高清视频 | www.青草视频| 视频二区 素人 制服 国产 | 激情偷拍网 | 农夫69小说小雨与农村老太 | 色小孩导航 | 黄www片 | 亚洲精品国精品久久99热 | 激情综合 | 精品国产一区二区三区在线观看 | 日本欧美强乱视频在线 | 麻豆视频免费在线播放 | 国产乱码一卡二卡3卡四卡 国产乱插 | 日韩欧美推理片免费看完整版 | 精品一区二区三区免费视频 | 青青久久久国产线免观 | 秋霞鲁丝影院久久人人综合 | 幸福草电视剧演员表介绍 | 91麻豆精品国产 | 国产自拍偷拍自拍 | 97精品国产自在现线免费观看 | 爱豆传媒最新视频国产 | 赤坂丽女医bd无删减在线观看 | 午夜精品在线视频 | 久久se视频精品视频在线 | 成年男女免费视频观看性 | 日韩高清在线免费看 | 久久这里只有精品视频e | 天天色综合久久 | 欧美一区二区三区四区视频 | 狠狠综合视频精品播放 | 亚洲性视频在线观看 | 2023毛片| 精品亚洲综合在线第一区 | 色人阁小说 |