溫馨提示×

溫馨提示×

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

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

基于ScheduledExecutorService的方法有哪些

發布時間:2021-08-11 15:34:03 來源:億速云 閱讀:154 作者:小新 欄目:編程語言

這篇文章主要介紹基于ScheduledExecutorService的方法有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

開發中,往往遇到另起線程執行其他代碼的情況,用java定時任務接口ScheduledExecutorService來實現。

ScheduledExecutorService是基于線程池設計的定時任務類,每個調度任務都會分配到線程池中的一個線程去執行,也就是說,任務是并發執行,互不影響。

注意,只有當調度任務來的時候,ScheduledExecutorService才會真正啟動一個線程,其余時間ScheduledExecutorService都是處于輪詢任務的狀態。

1.scheduleAtFixedRate方法

例子:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduleAtFixedRateDemo {

  public static void main(String[] args) {
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    SimpleDateFormat df = 
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
    
    executorService.scheduleAtFixedRate(new Runnable(){

      @Override
      public void run() {
        System.out.println("++++++++++++++++++++thread:" + df.format(new Date()));
      }
      
    }, 2, 3, TimeUnit.SECONDS);
    System.out.println("++++++++++++++++++++main:" + df.format(new Date()));
  }
}

運行結果:

++++++++++++++++++++main:2017-10-20 15:20:52
++++++++++++++++++++thread:2017-10-20 15:20:54
++++++++++++++++++++thread:2017-10-20 15:20:57
++++++++++++++++++++thread:2017-10-20 15:21:00
++++++++++++++++++++thread:2017-10-20 15:21:03
++++++++++++++++++++thread:2017-10-20 15:21:06

可以看出來,在2s后,子線程開始執行,并且每過3s輪詢執行一次。

2.scheduleWithFixedDelay方法

例子:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * ScheduleWithFixedDelay的用法
 * @author Administrator
 *
 */
public class ScheduleWithFixedDelayDemo {

  public static void main(String[] args) {
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    SimpleDateFormat df = 
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
    
    executorService.scheduleWithFixedDelay(new Runnable(){
      @Override
      public void run() {  
        System.out.println("++++++++++++++++++++thread:" + df.format(new Date()));
      }
      
    }, 2, 3, TimeUnit.SECONDS);
    
    System.out.println("++++++++++++++++++++main:" + df.format(new Date()));
  }
}

運行結果:

++++++++++++++++++++main:2017-10-20 15:24:32
++++++++++++++++++++thread:2017-10-20 15:24:34
++++++++++++++++++++thread:2017-10-20 15:24:37
++++++++++++++++++++thread:2017-10-20 15:24:40
++++++++++++++++++++thread:2017-10-20 15:24:43

3.兩個區別

ScheduleAtFixedRate每次執行時間為上一次任務開始起向后推一個時間間隔,即每次執行時間為initialDelay,initialDelay+period,initialDelay+2*period。。。。。

ScheduleWithFixedDelay每次執行時間為上一次任務結束起向后推一個時間間隔,即每次執行時間為:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。

由此可見,ScheduleAtFixedRate是基于固定時間間隔進行任務調度,ScheduleWithFixedDelay取決于每次任務執行的時間長短,是基于不固定時間間隔進行任務調度。

以上是“基于ScheduledExecutorService的方法有哪些”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女