getwayworker timer_Java定时器之Timer学习二
方法名稱 schedule() 和 scheduleAtFixedRate() 的區(qū)別
兩種情況看區(qū)別
首次計(jì)劃執(zhí)行的時(shí)間早于當(dāng)前時(shí)間
比如說(shuō):當(dāng)前時(shí)間是 11:06, 但是首次計(jì)劃執(zhí)行的時(shí)間應(yīng)該為: 11:00
任務(wù)執(zhí)行所需的時(shí)間超出任務(wù)的執(zhí)行周期間隔
比如說(shuō):我們執(zhí)行的任務(wù)的時(shí)間為 3秒,但是任務(wù)執(zhí)行的周期間隔為 2秒。
詳細(xì)分析
首次計(jì)劃的時(shí)間早于當(dāng)前時(shí)間
schedule 方法
fixed-delay; 如果第一次執(zhí)行的時(shí)間被 delay 了,隨后的執(zhí)行時(shí)間按照上一次實(shí)際執(zhí)行完成的時(shí)間點(diǎn)進(jìn)行計(jì)算。
代碼實(shí)例
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class DifferenceTest {
public static void main(String[] args) {
// 獲取當(dāng)前時(shí)間按照指定的格式輸出
final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
System.out.println("current time is:"+ sf.format(calendar.getTime()));
// 設(shè)置成6秒之前的時(shí)間
calendar.add(Calendar.SECOND, -6);
System.out.println("current time minus six second is :"+ sf.format(calendar.getTime()));
System.out.println("Task is being executed!");
// 使用 timer 來(lái)執(zhí)行
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));
}
}, calendar.getTime(), 2000);
}
}
控制臺(tái)輸出
current time is:2018-07-05 13:09:57
current time minus six second is :2018-07-05 13:09:51
scheduled exec time is :2018-07-05 13:09:57
scheduled exec time is :2018-07-05 13:09:59
scheduled exec time is :2018-07-05 13:10:01
scheduled exec time is :2018-07-05 13:10:03
scheduled exec time is :2018-07-05 13:10:05
scheduled exec time is :2018-07-05 13:10:07
scheduled exec time is :2018-07-05 13:10:09
schedule 方法總結(jié)
雖然我們是將事件提前了6秒,但是使用 schedule 還是從當(dāng)前時(shí)間開(kāi)始執(zhí)行。然后每隔兩秒執(zhí)行一次。
scheduleAtFixedRate 方法
fixed-rate; 如果第一次執(zhí)行時(shí)間被 delay了,隨后的執(zhí)行時(shí)間按照上一次開(kāi)始的點(diǎn)計(jì)算,并且為了趕上進(jìn)度會(huì)多次執(zhí)行任務(wù),因?yàn)?TimerTask中的執(zhí)行需要考慮同步。
代碼實(shí)例
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class DifferenceTest {
public static void main(String[] args) {
// 獲取當(dāng)前時(shí)間按照指定的格式輸出
final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
System.out.println("current time is:"+ sf.format(calendar.getTime()));
// 設(shè)置成6秒之前的時(shí)間
calendar.add(Calendar.SECOND, -6);
System.out.println("current time minus six second is :"+ sf.format(calendar.getTime()));
// 使用 timer 來(lái)執(zhí)行
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));
}
}, calendar.getTime(), 2000);
}
}
控制臺(tái)輸出
current time is:2018-07-06 14:32:34
current time minus six second is :2018-07-06 14:32:28
scheduled exec time is :2018-07-06 14:32:28
scheduled exec time is :2018-07-06 14:32:30
scheduled exec time is :2018-07-06 14:32:32
scheduled exec time is :2018-07-06 14:32:34
scheduled exec time is :2018-07-06 14:32:36
scheduled exec time is :2018-07-06 14:32:38
scheduleAtFixedRate 方法總結(jié)
我們可以看到實(shí)際的效果是:在啟動(dòng)執(zhí)行的時(shí)候,會(huì)立馬執(zhí)行3次,就是為了追趕已經(jīng)過(guò)去的6秒。然后再按照設(shè)定的間隔,每?jī)擅腌妶?zhí)行一次。
任務(wù)執(zhí)行所需的時(shí)間超出任務(wù)的執(zhí)行周期間隔
schedule 方法
下次執(zhí)行時(shí)間相當(dāng)于上一次實(shí)際執(zhí)行完成的時(shí)間點(diǎn),因?yàn)閳?zhí)行的時(shí)間會(huì)不斷延后。
代碼實(shí)例
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class DifferenceTest {
public static void main(String[] args) {
// 獲取當(dāng)前時(shí)間按照指定的格式輸出
final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
System.out.println("current time is:"+ sf.format(calendar.getTime()));
// 使用 timer 來(lái)執(zhí)行
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// 模擬當(dāng)前執(zhí)行的過(guò)程需要 3秒
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 打印最近一次執(zhí)行的時(shí)間
System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));
}
}, calendar.getTime(), 2000);
}
}
控制臺(tái)輸出
current time is:2018-07-06 14:43:51
scheduled exec time is :2018-07-06 14:43:51
scheduled exec time is :2018-07-06 14:43:54
scheduled exec time is :2018-07-06 14:43:57
scheduled exec time is :2018-07-06 14:44:00
scheduled exec time is :2018-07-06 14:44:03
說(shuō)明
我們可以空控制臺(tái)中輸出的結(jié)果中看到:我們當(dāng)前的時(shí)間為 14:43:51,然后第一次計(jì)劃執(zhí)行的時(shí)間也為 14:43:51。但是以后每次執(zhí)行的時(shí)間都是相隔 3秒鐘,并不是我們上面設(shè)置 timerTask 的時(shí)間間隔 2秒。所以說(shuō)使用 schedule 方法,在這種情況下會(huì)不斷的延后。
scheduleAtFixedRate 方法
下一次執(zhí)行時(shí)間相對(duì)于上一次開(kāi)始的時(shí)間點(diǎn),因此執(zhí)行時(shí)間一般不會(huì)延后,因此存在并發(fā)性
代碼實(shí)例
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
public class DifferenceTest {
public static void main(String[] args) {
// 獲取當(dāng)前時(shí)間按照指定的格式輸出
final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
System.out.println("current time is:"+ sf.format(calendar.getTime()));
// 使用 timer 來(lái)執(zhí)行
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// 模擬當(dāng)前執(zhí)行的過(guò)程需要 3秒
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 打印最近一次執(zhí)行的時(shí)間
System.out.println("scheduled exec time is :"+ sf.format(scheduledExecutionTime()));
}
}, calendar.getTime(), 2000);
}
}
控制臺(tái)輸出
current time is:2018-07-07 10:15:51
scheduled exec time is :2018-07-07 10:15:51
scheduled exec time is :2018-07-07 10:15:53
scheduled exec time is :2018-07-07 10:15:55
scheduled exec time is :2018-07-07 10:15:57
scheduled exec time is :2018-07-07 10:15:59
說(shuō)明
當(dāng)執(zhí)行的頻率為2秒鐘,但是執(zhí)行的時(shí)間為3秒的時(shí)。我們從控制臺(tái)上的輸出可以看到,執(zhí)行的頻率還是為2秒,因此就會(huì)存在并發(fā)性。
總結(jié)
以上是生活随笔為你收集整理的getwayworker timer_Java定时器之Timer学习二的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: curaengine linux编译,C
- 下一篇: android+录像中截图软件下载,录屏