溫馨提示×

溫馨提示×

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

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

Java如何使用ExecutorService停止線程服務(wù)的方法

發(fā)布時間:2021-03-10 15:44:52 來源:億速云 閱讀:537 作者:TREX 欄目:編程語言

這篇文章主要介紹“Java如何使用ExecutorService停止線程服務(wù)的方法”,在日常操作中,相信很多人在Java如何使用ExecutorService停止線程服務(wù)的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java如何使用ExecutorService停止線程服務(wù)的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

Java可以用來干什么

Java主要應(yīng)用于:1. web開發(fā);2. Android開發(fā);3. 客戶端開發(fā);4. 網(wǎng)頁開發(fā);5. 企業(yè)級應(yīng)用開發(fā);6. Java大數(shù)據(jù)開發(fā);7.游戲開發(fā)等。


使用ExecutorService來停止線程服務(wù)

之前的文章中我們提到了ExecutorService可以使用shutdown和shutdownNow來關(guān)閉。

這兩種關(guān)閉的區(qū)別在于各自的安全性和響應(yīng)性。shutdownNow強行關(guān)閉速度更快,但是風(fēng)險也更大,因為任務(wù)可能正在執(zhí)行的過程中被結(jié)束了。而shutdown正常關(guān)閉雖然速度比較慢,但是卻更安全,因為它一直等到隊列中的所有任務(wù)都執(zhí)行完畢之后才關(guān)閉。

使用shutdown

我們先看一個使用shutdown的例子:

  public void useShutdown() throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(10);

    Runnable runnableTask = () -> {
      try {
        TimeUnit.MILLISECONDS.sleep(300);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    };

    executor.submit(runnableTask);
    executor.shutdown();
    executor.awaitTermination(800, TimeUnit.MILLISECONDS);
  }

awaitTermination將會阻塞直到所有正在執(zhí)行的任務(wù)完成,或者達到指定的timeout時間。

使用shutdownNow

當(dāng)通過shutdownNow來強行關(guān)閉ExecutorService是, 它會嘗試取消正在執(zhí)行的任務(wù),并返回所有已經(jīng)提交但是還沒有開始的任務(wù)。從而可以將這些任務(wù)保存起來,以便以后進行處理。

但是這樣我們只知道了還沒有開始執(zhí)行的任務(wù),對于那些已經(jīng)開始執(zhí)行但是沒有執(zhí)行完畢卻被取消的任務(wù)我們無法獲取。

我們看下如何獲得開始執(zhí)行但是還沒有執(zhí)行完畢的任務(wù):

public class TrackingExecutor extends AbstractExecutorService {
  private final ExecutorService executorService;
  private final Set<Runnable> taskCancelledAtShutdown= Collections.synchronizedSet(new HashSet<Runnable>());

  public TrackingExecutor(ExecutorService executorService){
     this.executorService=executorService;
  }
  @Override
  public void shutdown() {
    executorService.shutdown();
  }

  @Override
  public List<Runnable> shutdownNow() {
    return executorService.shutdownNow();
  }

  @Override
  public boolean isShutdown() {
    return executorService.isShutdown();
  }

  @Override
  public boolean isTerminated() {
    return executorService.isTerminated();
  }

  @Override
  public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
    return executorService.awaitTermination(timeout,unit);
  }

  @Override
  public void execute(Runnable command) {
    executorService.execute(() -> {
      try {
        command.run();
      }finally {
        if(isShutdown() && Thread.currentThread().isInterrupted()){
          taskCancelledAtShutdown.add(command);
        }
      }
    });
  }

  public List<Runnable> getCancelledTask(){
    if(! executorService.isTerminated()){
      throw new IllegalStateException("executorService is not terminated");
    }
    return new ArrayList<>(taskCancelledAtShutdown);
  }
}

上面的例子中我們構(gòu)建了一個新的ExecutorService,他傳入一個ExecutorService,并對其進行封裝。

我們重寫了execute方法,在執(zhí)行完畢判斷該任務(wù)是否被中斷,如果被中斷則將其添加到CancelledTask列表中。

并提供一個getCancelledTask方法來返回未執(zhí)行完畢的任務(wù)。

我們看下怎么使用:

  public void useShutdownNow() throws InterruptedException {
    TrackingExecutor trackingExecutor=new TrackingExecutor(Executors.newCachedThreadPool());

    Runnable runnableTask = () -> {
      try {
        TimeUnit.MILLISECONDS.sleep(300);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    };

    trackingExecutor.submit(runnableTask);
    List<Runnable> notrunList=trackingExecutor.shutdownNow();
    if(trackingExecutor.awaitTermination(800, TimeUnit.SECONDS)){
      List<Runnable> runButCancelledList= trackingExecutor.getCancelledTask();
    }
  }

trackingExecutor.shutdownNow()返回的是未執(zhí)行的任務(wù)。而trackingExecutor.getCancelledTask()返回的是被取消的任務(wù)。

上面的任務(wù)其實還有一個缺點,因為我們在存儲被取消的任務(wù)列表的額時候taskCancelledAtShutdown.add(command),因為之前的判斷不是原子操作,則可能會產(chǎn)生誤報。

到此,關(guān)于“Java如何使用ExecutorService停止線程服務(wù)的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

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

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

AI