国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

ScheduledExecutorService任務定時代碼示例

2024-07-13 10:16:23
字體:
來源:轉載
供稿:網友

本文主要分享了一則關于ScheduledExecutorService任務定時的實例代碼,具體如下:

示例代碼

package com.effective.common.concurrent.execute;import java.text.DateFormat;import java.text.ParseException;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 Schedule {	private static DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");	private static DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");	private static ScheduledExecutorService excutor = Executors.newSingleThreadScheduledExecutor();	/**   * 按指定頻率周期執行某個任務 <br>   * 初始化延遲0ms開始執行,每隔5ms重新執行一次任務。   */	public void fixedRate(){		excutor.scheduleAtFixedRate(new EchoServer(), //執行線程		0, //初始化延遲		5000, //兩次開始的執行的最小時間間隔		TimeUnit.MILLISECONDS //計時單位		);	}	/**   *    */	public void fixDelay(){		excutor.scheduleWithFixedDelay(new EchoServer(),//執行線程 		0, //初始化延遲		5000, //前一次執行結束到下一次執行開始的間隔時間		TimeUnit.MILLISECONDS);	}	/**   * 每天晚上8點執行一次   */	public void dayOfDelay(String time){		ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);		long oneDay = 24 * 60 * 60 * 1000;		long initDelay = getTimeMillis("20:00:00") - System.currentTimeMillis();		initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;		executor.scheduleAtFixedRate( 		        new EchoServer(), 		        initDelay, 		        oneDay, 		        TimeUnit.MILLISECONDS);	}	/**   * 獲取給定時間對應的毫秒數   * @param string "HH:mm:ss"   * @return   */	private static long getTimeMillis(String time) {		try {			Date currentDate = dateFormat.parse(dayFormat.format(new Date()) + " " +time);			return currentDate.getTime() ;		}		catch (ParseException e) {			e.printStackTrace();		}		return 0;	}	public static void main(String[] args){		Schedule schedule = new Schedule();		schedule.fixedRate();		schedule.fixDelay();	}}

總結

以上就是本文關于ScheduledExecutorService任務定時代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嵊州市| 屏东市| 新邵县| 凌源市| 莎车县| 平度市| 孟州市| 武清区| 成武县| 万源市| 平和县| 皮山县| 纳雍县| 开江县| 京山县| 睢宁县| 新泰市| 安义县| 芜湖县| 武邑县| 南溪县| 葫芦岛市| 孟津县| 延长县| 塘沽区| 连云港市| 锦屏县| 太保市| 山西省| 唐山市| 银川市| 阿巴嘎旗| 北流市| 若尔盖县| 高邑县| 灵山县| 尚志市| 兴宁市| 彭水| 海南省| 左权县|