欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

理解java多線程中ExecutorService使用

 更新時(shí)間:2016年12月01日 11:50:39   作者:stefan.king  
這篇文章主要幫助大家理解java多線程中ExcetorServiced的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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ù)(線程),只有一個(gè)方法 execute(Runnable a)

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

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

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

對(duì)ExecutorService和Future的理解做簡(jiǎn)單記錄

代碼:

public class Main {
  private static int count = 0;

  public static void main(String[] args){
    List<Future> resultList = new LinkedList<>();

    /**
     * Executors.newCachedThreadPool() 創(chuàng)建一個(gè)線程緩存池,若60s中線程沒有被使用,則會(huì)停止線程并從緩存池中移除
     * Executors.newScheduledThreadPool() 創(chuàng)建一個(gè)固定容量的線程池,里邊的線程按照設(shè)定的調(diào)度時(shí)間執(zhí)行
     * Executors.newFixedThreadPool()  擁有固定容量的線程緩存池
     * Executors.newSingleThreadExecutor() 容量為一的線程緩存池,只會(huì)有一個(gè)線程
     */
    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...");
  }
}

輸出:

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é)束

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

總結(jié):

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

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

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

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

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

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

補(bǔ)充:

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JDBC中使用Java8的日期LocalDate和LocalDateTime操作mysql、postgresql

    JDBC中使用Java8的日期LocalDate和LocalDateTime操作mysql、postgresql

    這篇文章主要給大家介紹了關(guān)于JDBC中如何使用Java8的日期LocalDate和LocalDateTime的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用

    Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用

    今天小編就為大家分享一篇關(guān)于Spring Boot配置Thymeleaf(gradle)的簡(jiǎn)單使用,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 網(wǎng)關(guān)Gateway過濾器的使用詳解

    網(wǎng)關(guān)Gateway過濾器的使用詳解

    Gateway網(wǎng)關(guān)的過濾器分為兩種,一種是局部過濾器,一種是全局過濾器,過濾器就是過濾一些請(qǐng)求,在這里,全局過濾器的作用是處理一切進(jìn)入網(wǎng)關(guān)的請(qǐng)求和微服務(wù)響應(yīng),與GatewayFilter的作用一樣,本文給大家介紹網(wǎng)關(guān)Gateway過濾器的使用,感興趣的朋友一起看看吧
    2022-07-07
  • 詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫

    詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫

    這篇文章主要介紹了詳解MybatisPlus集成nacos導(dǎo)致druid連接不上數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐

    Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐

    在現(xiàn)代電子商務(wù)網(wǎng)站中,商品詳情API是一個(gè)重要的組件,本文就來介紹一下Java調(diào)用商品詳情API的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解

    Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解

    這篇文章主要介紹了Spring中bean對(duì)象的裝配方式、作用域及生命周期詳解,SprignBoot中?@Bean?完美的替換了了上面的這種在xml中配置的方法,使用以下方法就能讓spring在需要自動(dòng)創(chuàng)建Info對(duì)象時(shí),自動(dòng)調(diào)用這個(gè)方法,需要的朋友可以參考下
    2023-11-11
  • 如何用java給文件加密的簡(jiǎn)單實(shí)現(xiàn)

    如何用java給文件加密的簡(jiǎn)單實(shí)現(xiàn)

    文件加密,簡(jiǎn)單來說就是把文件讀取出來,把讀取出來的字節(jié)碼數(shù)組進(jìn)行遍歷,把每一個(gè)碼值和一個(gè)秘鑰(隨便一個(gè)數(shù))進(jìn)行異或運(yùn)算,將運(yùn)算后的結(jié)果全部寫入到文件里,這篇文章主要介紹了如何用java給文件加密的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下
    2023-12-12
  • Java開發(fā)工具-scala處理json格式利器-json4s詳解

    Java開發(fā)工具-scala處理json格式利器-json4s詳解

    這篇文章主要介紹了開發(fā)工具-scala處理json格式利器-json4s,文章中處理方法講解的很清楚,有需要的同學(xué)可以研究下
    2021-02-02
  • SpringBoot的@GetMapping路徑匹配規(guī)則、國際化詳細(xì)教程

    SpringBoot的@GetMapping路徑匹配規(guī)則、國際化詳細(xì)教程

    這篇文章主要介紹了SpringBoot的@GetMapping路徑匹配規(guī)則、國際化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-11-11
  • java判斷各類型字符個(gè)數(shù)實(shí)例代碼

    java判斷各類型字符個(gè)數(shù)實(shí)例代碼

    大家好,本篇文章主要講的是java判斷各類型字符個(gè)數(shù)實(shí)例代碼,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12

最新評(píng)論