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

基于ScheduledExecutorService的兩種方法(詳解)

 更新時間:2017年10月21日 10:34:08   作者:面條啊Andrew  
下面小編就為大家?guī)硪黄赟cheduledExecutorService的兩種方法(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

開發(fā)中,往往遇到另起線程執(zhí)行其他代碼的情況,用java定時任務(wù)接口ScheduledExecutorService來實現(xiàn)。

ScheduledExecutorService是基于線程池設(shè)計的定時任務(wù)類,每個調(diào)度任務(wù)都會分配到線程池中的一個線程去執(zhí)行,也就是說,任務(wù)是并發(fā)執(zhí)行,互不影響。

注意,只有當(dāng)調(diào)度任務(wù)來的時候,ScheduledExecutorService才會真正啟動一個線程,其余時間ScheduledExecutorService都是處于輪詢?nèi)蝿?wù)的狀態(tài)。

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");//設(shè)置日期格式
    
    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()));
  }
}

運行結(jié)果:

++++++++++++++++++++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后,子線程開始執(zhí)行,并且每過3s輪詢執(zhí)行一次。

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");//設(shè)置日期格式
    
    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()));
  }
}

運行結(jié)果:

++++++++++++++++++++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.兩個區(qū)別

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

ScheduleWithFixedDelay每次執(zhí)行時間為上一次任務(wù)結(jié)束起向后推一個時間間隔,即每次執(zhí)行時間為:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。

由此可見,ScheduleAtFixedRate是基于固定時間間隔進行任務(wù)調(diào)度,ScheduleWithFixedDelay取決于每次任務(wù)執(zhí)行的時間長短,是基于不固定時間間隔進行任務(wù)調(diào)度。

以上這篇基于ScheduledExecutorService的兩種方法(詳解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring如何使用PropertyPlaceholderConfigurer讀取文件

    Spring如何使用PropertyPlaceholderConfigurer讀取文件

    這篇文章主要介紹了Spring如何使用PropertyPlaceholderConfigurer讀取文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-12-12
  • 詳解spring多線程與定時任務(wù)

    詳解spring多線程與定時任務(wù)

    本篇文章主要介紹了spring多線程與定時任務(wù),詳細的介紹了spring多線程任務(wù)和spring定時任務(wù),有興趣的可以了解一下。
    2017-04-04
  • Java中單例模式的7種寫法

    Java中單例模式的7種寫法

    這篇文章主要介紹了Java中單例模式的7種寫法,本文分別給出每種方式的實現(xiàn)代碼,需要的朋友可以參考下
    2015-05-05
  • java連接MySQl數(shù)據(jù)庫實例代碼

    java連接MySQl數(shù)據(jù)庫實例代碼

    這篇文章介紹了java連接MySQl數(shù)據(jù)庫實例代碼,有需要的朋友可以參考一下
    2013-10-10
  • Java序列化常見的三個問題

    Java序列化常見的三個問題

    這篇文章主要介紹了Java序列化常見的三個問題,幫助大家更好的理解和學(xué)習(xí)JAVA,感興趣的朋友可以了解下
    2020-08-08
  • SpringCloud微服務(wù)應(yīng)用config配置中心詳解

    SpringCloud微服務(wù)應(yīng)用config配置中心詳解

    這篇文章主要介紹了SpringCloud微服務(wù)應(yīng)用-config配置中心,包括相關(guān)知識介紹、搭建、動態(tài)刷新、測試,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Netty實戰(zhàn)源碼解析NIO編程

    Netty實戰(zhàn)源碼解析NIO編程

    這篇文章主要為大家介紹了Netty實戰(zhàn)源碼解析NIO編程的核心組件及關(guān)系詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • java線程并發(fā)blockingqueue類使用示例

    java線程并發(fā)blockingqueue類使用示例

    BlockingQueue是一種特殊的Queue,若BlockingQueue是空的,從BlockingQueue取東西的操作將會被阻斷進入等待狀態(tài)直到BlocingkQueue進了新貨才會被喚醒,下面是用BlockingQueue來實現(xiàn)Producer和Consumer的例子
    2014-01-01
  • 數(shù)據(jù)同步利器DataX簡介及如何使用

    數(shù)據(jù)同步利器DataX簡介及如何使用

    DataX?是阿里云?DataWorks數(shù)據(jù)集成?的開源版本,使用Java?語言編寫,在阿里巴巴集團內(nèi)被廣泛使用的離線數(shù)據(jù)同步工具/平臺,今天給大家分享一個阿里開源的數(shù)據(jù)同步工具DataX,在Github擁有14.8k的star,非常受歡迎
    2024-02-02
  • 解決spring-integration-mqtt頻繁報Lost connection錯誤問題

    解決spring-integration-mqtt頻繁報Lost connection錯誤問題

    這篇文章主要介紹了解決spring-integration-mqtt頻繁報Lost connection錯誤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論