Java 定時(shí)器的使用示例
最近要用到定時(shí)任務(wù),就是超過48小時(shí)的數(shù)據(jù)給關(guān)閉,前臺(tái)就不顯示了。還是頭一次使用java的定時(shí)器,。。java定時(shí)器使用Timer類。有時(shí)間得看下源碼了,看看具體咋弄的。
Timer主要用于Java線程里指定時(shí)間或周期運(yùn)行任務(wù)。Timer是線程安全的,但不提供實(shí)時(shí)性(real-time)保證。先看看調(diào)用timer的幾種方式。
/** * 這個(gè)方法是調(diào)度一個(gè)task,經(jīng)過2000(ms)后開始進(jìn)行調(diào)度,僅僅調(diào)度一次。 */ public static void timer1(){ Timer nTimer = new Timer(); nTimer.schedule(new TimerTask() { @Override public void run() {System.out.println('----設(shè)定要指定任務(wù)-----'); } },2000); } /** * 在指定的時(shí)間點(diǎn)time上調(diào)度一次。 */ public static void timer2() { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() {System.out.println('-------延遲5000毫秒,每1000毫秒執(zhí)行一次--------'); } }, time); } /** * 延遲5000毫秒,每1000毫秒執(zhí)行一次 * 在5000(ms)后開始調(diào)度,每次調(diào)度完后,最少等待1000(ms)后才開始調(diào)度。 */ public static void timer3() { Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() {System.err.println('-------延遲5000毫秒,每1000毫秒執(zhí)行一次--------'); } }, 5000, 1000); } /** * 設(shè)置17:56執(zhí)行任務(wù)(第一次調(diào)度的時(shí)間),每過一天執(zhí)行一次 * java.util.Timer.scheduleAtFixedRate(TimerTask task, Date firstTime, long period) */ public static void timer4() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 17); calendar.set(Calendar.MINUTE, 26); calendar.set(Calendar.SECOND, 0); Date time = calendar.getTime(); Timer timer = new Timer(); timer.scheduleAtFixedRate(new TimerTask() { public void run() {System.out.println('-------設(shè)定要指定任務(wù)--------'); } }, time, 1000 * 60 * 60 * 24);// 這里設(shè)定將延時(shí)每天固定執(zhí)行 }
以上就是調(diào)用的幾種方法,再貼下我的代碼
// 時(shí)間間隔(一天) long periodDay = 24 * 60 * 60 * 1000; Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 1); // 凌晨1點(diǎn) calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date date = calendar.getTime(); // 第一次執(zhí)行定時(shí)任務(wù)的時(shí)間 // 如果第一次執(zhí)行定時(shí)任務(wù)的時(shí)間 小于當(dāng)前的時(shí)間 // 此時(shí)要在 第一次執(zhí)行定時(shí)任務(wù)的時(shí)間加一天,以便此任務(wù)在下個(gè)時(shí)間點(diǎn)執(zhí)行。如果不加一天,任務(wù)會(huì)立即執(zhí)行。 if (date.before(new Date())) { date = this.addDay(date, 1); } new Timer().scheduleAtFixedRate(new TimerTask() { public void run() {Date now = new Date();//查出所有未關(guān)閉數(shù)據(jù)List<BankWork> list = bankWorkService.findList(new BankWork().setStatus(Const.DEFAULT_STATUS));//篩選出超過48小時(shí)的未關(guān)閉數(shù)據(jù)list = list.stream().filter(e -> { try { return judgmentDate(e.getModifiedTime(), now); } catch (Exception e1) { e1.printStackTrace(); } return false;}).collect(Collectors.toList());list.forEach(e -> e.setStatus(Const.BANK_WORK_STATUS_OK).setModifiedTime(now));//修改所有未關(guān)閉數(shù)據(jù)為關(guān)閉bankWorkService.updateAsGroup(list); } }, date, periodDay); // 判斷時(shí)間是否超過48小時(shí) private static boolean judgmentDate(Date date1, Date date2) throws Exception { long cha = date2.getTime() - date1.getTime(); if (cha < 0) { return false; } double result = cha * 1.0 / (1000 * 60 * 60); if (result <= 48) { return true; } else { return false; } } // 增加或減少天數(shù) private Date addDay(Date date, int num) { Calendar startDT = Calendar.getInstance(); startDT.setTime(date); startDT.add(Calendar.DAY_OF_MONTH, num); return startDT.getTime(); }
以上就是Java 定時(shí)器的使用示例的詳細(xì)內(nèi)容,更多關(guān)于Java 定時(shí)器的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. ASP基礎(chǔ)知識(shí)VBScript基本元素講解2. ajax請求添加自定義header參數(shù)代碼3. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)4. IntelliJ IDEA導(dǎo)入jar包的方法5. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫6. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法7. 使用Python和百度語音識(shí)別生成視頻字幕的實(shí)現(xiàn)8. 使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼9. python操作mysql、excel、pdf的示例10. vue-electron中修改表格內(nèi)容并修改樣式
