溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java項(xiàng)目中的線程池異步怎么利用 ExecutorServic實(shí)現(xiàn)

發(fā)布時(shí)間:2020-12-01 14:56:06 來源:億速云 閱讀:271 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)Java項(xiàng)目中的線程池異步怎么利用 ExecutorServic實(shí)現(xiàn),可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Java通過Executors提供四種線程池,分別為:

  • newCachedThreadPool創(chuàng)建一個(gè)可緩存線程池,如果線程池長度超過處理需要,可靈活回收空閑線程,若無可回收,則新建線程。

  • newFixedThreadPool 創(chuàng)建一個(gè)定長線程池,可控制線程最大并發(fā)數(shù),超出的線程會(huì)在隊(duì)列中等待。

  • newScheduledThreadPool 創(chuàng)建一個(gè)定長線程池,支持定時(shí)及周期性任務(wù)執(zhí)行。

  • newSingleThreadExecutor 創(chuàng)建一個(gè)單線程化的線程池,它只會(huì)用唯一的工作線程來執(zhí)行任務(wù),保證所有任務(wù)按照指定順序(FIFO, LIFO, 優(yōu)先級)執(zhí)行。

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author szy 
 * @version 創(chuàng)建時(shí)間:2018-5-20 上午10:25:06
 * 
 */
public class Testasync {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    if(task0() == true){
      System.out.println("執(zhí)行完畢,看異步結(jié)果");
    }
    
  }

  
  public static void task1(){
    System.out.println("task1 is start");
  }
  
  public static void task2(){
     ExecutorService executor = Executors.newFixedThreadPool(1);
     executor.submit(new Callable(){

      @Override
      public Object call() throws Exception {
        // TODO Auto-generated method stub
        
        //增加睡眠時(shí)間,便于查看結(jié)果
        /* try {
            Thread.sleep(10000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }*/
         
        //異步提交
        int sum = 0;
        for (int i = 0; i < 10000; i++) {
           sum += i;
        }
        System.out.println("task2執(zhí)行數(shù)據(jù)的大量導(dǎo)入或者導(dǎo)出");
        System.out.println("task2="+sum);
        System.out.println("task2導(dǎo)入或者導(dǎo)出完成");
        return null;
      }
       
       
     });
    
  }
  
  public static void task3(){
    System.out.println("task3 is start");
    int j = 0;
    while(true) {
      if(j++ > 10) {
        break;
      }
      System.out.println("------------task3 end-----------");
    }
  }
  
  public static boolean task0(){
    task1();
    task2();
    task3();
    return true;
  }
}

然后看結(jié)果:

task1 is start
task3 is start
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
------------task3 end-----------
task2執(zhí)行數(shù)據(jù)的大量導(dǎo)入或者導(dǎo)出
執(zhí)行完畢,看異步結(jié)果
task2=49995000
task2導(dǎo)入或者導(dǎo)出完成

可以看出,task1 和task3先執(zhí)行了,并且方法在沒有等待task2的情況下,直接結(jié)束了。

異步的task2另開了一個(gè)線程,自己在執(zhí)行。和主線程已經(jīng)無關(guān)了。

不過,這種在eclipse中以deubug模式是看不出來的。

看完上述內(nèi)容,你們對Java項(xiàng)目中的線程池異步怎么利用 ExecutorServic實(shí)現(xiàn)有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI