How to understand ` catch up` of Java Timer `scheduleAtFixedRate` method and `schedule` method actual run...
java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
the doc say:
If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."
And I do a test, code as follow:
public static void main(String args) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 实际运行时间:%s,需要运行时间:%s", new Date(), new Date(this.scheduledExecutionTime())));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 1 实际运行时间:%s,需要运行时间:%s", new Date().getTime(), new Date(this.scheduledExecutionTime())));
}
};
System.out.println("开始执行时间:" + new Date());
timer.schedule(timerTask, 0, 1000);//
timer.scheduleAtFixedRate(timerTask1, 0, 1000);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
an example of results:
开始执行时间:Tue Nov 20 15:56:50 CST 2018 --line 1
Task 实际运行时间:Tue Nov 20 15:56:50 CST 2018,需要运行时间:Tue Nov 20 15:56:50 CST 2018 --line 2
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:50 CST 2018,计时器:1 --line 3
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:51 CST 2018,计时器:2 --line 4
Task 实际运行时间:Tue Nov 20 15:56:53 CST 2018,需要运行时间:Tue Nov 20 15:56:53 CST 2018 --line 5
line 3,4 实际运行时间 is same time,and how to understand the catch up
upon the method doc?
line 2,5 The Task
run time need at less 3 second,and period
is 1 second ,and the console why not per 1 second?
java
add a comment |
java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
the doc say:
If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."
And I do a test, code as follow:
public static void main(String args) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 实际运行时间:%s,需要运行时间:%s", new Date(), new Date(this.scheduledExecutionTime())));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 1 实际运行时间:%s,需要运行时间:%s", new Date().getTime(), new Date(this.scheduledExecutionTime())));
}
};
System.out.println("开始执行时间:" + new Date());
timer.schedule(timerTask, 0, 1000);//
timer.scheduleAtFixedRate(timerTask1, 0, 1000);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
an example of results:
开始执行时间:Tue Nov 20 15:56:50 CST 2018 --line 1
Task 实际运行时间:Tue Nov 20 15:56:50 CST 2018,需要运行时间:Tue Nov 20 15:56:50 CST 2018 --line 2
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:50 CST 2018,计时器:1 --line 3
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:51 CST 2018,计时器:2 --line 4
Task 实际运行时间:Tue Nov 20 15:56:53 CST 2018,需要运行时间:Tue Nov 20 15:56:53 CST 2018 --line 5
line 3,4 实际运行时间 is same time,and how to understand the catch up
upon the method doc?
line 2,5 The Task
run time need at less 3 second,and period
is 1 second ,and the console why not per 1 second?
java
It doesn't answer your question but aTimerTask
should be fast in order to maintain the scheduling. In other words, it should not take seconds. In case your task has a lot of work to do, create a new thread within therun
method of your timertask.
– Robert Kock
Nov 20 '18 at 9:31
@Robert Kock ,thank you for your reply,I agree with your opinion.
– zhiwen zhang
Nov 21 '18 at 7:19
add a comment |
java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
the doc say:
If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."
And I do a test, code as follow:
public static void main(String args) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 实际运行时间:%s,需要运行时间:%s", new Date(), new Date(this.scheduledExecutionTime())));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 1 实际运行时间:%s,需要运行时间:%s", new Date().getTime(), new Date(this.scheduledExecutionTime())));
}
};
System.out.println("开始执行时间:" + new Date());
timer.schedule(timerTask, 0, 1000);//
timer.scheduleAtFixedRate(timerTask1, 0, 1000);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
an example of results:
开始执行时间:Tue Nov 20 15:56:50 CST 2018 --line 1
Task 实际运行时间:Tue Nov 20 15:56:50 CST 2018,需要运行时间:Tue Nov 20 15:56:50 CST 2018 --line 2
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:50 CST 2018,计时器:1 --line 3
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:51 CST 2018,计时器:2 --line 4
Task 实际运行时间:Tue Nov 20 15:56:53 CST 2018,需要运行时间:Tue Nov 20 15:56:53 CST 2018 --line 5
line 3,4 实际运行时间 is same time,and how to understand the catch up
upon the method doc?
line 2,5 The Task
run time need at less 3 second,and period
is 1 second ,and the console why not per 1 second?
java
java.util.Timer#scheduleAtFixedRate(java.util.TimerTask, long, long)
the doc say:
If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up."
And I do a test, code as follow:
public static void main(String args) {
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 实际运行时间:%s,需要运行时间:%s", new Date(), new Date(this.scheduledExecutionTime())));
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
TimerTask timerTask1 = new TimerTask() {
@Override
public void run() {
System.out.println(String.format("Task 1 实际运行时间:%s,需要运行时间:%s", new Date().getTime(), new Date(this.scheduledExecutionTime())));
}
};
System.out.println("开始执行时间:" + new Date());
timer.schedule(timerTask, 0, 1000);//
timer.scheduleAtFixedRate(timerTask1, 0, 1000);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
an example of results:
开始执行时间:Tue Nov 20 15:56:50 CST 2018 --line 1
Task 实际运行时间:Tue Nov 20 15:56:50 CST 2018,需要运行时间:Tue Nov 20 15:56:50 CST 2018 --line 2
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:50 CST 2018,计时器:1 --line 3
Task 1 实际运行时间:1542700613673,需要运行时间:Tue Nov 20 15:56:51 CST 2018,计时器:2 --line 4
Task 实际运行时间:Tue Nov 20 15:56:53 CST 2018,需要运行时间:Tue Nov 20 15:56:53 CST 2018 --line 5
line 3,4 实际运行时间 is same time,and how to understand the catch up
upon the method doc?
line 2,5 The Task
run time need at less 3 second,and period
is 1 second ,and the console why not per 1 second?
java
java
edited Nov 20 '18 at 7:58
zhiwen zhang
asked Nov 20 '18 at 7:45


zhiwen zhangzhiwen zhang
208419
208419
It doesn't answer your question but aTimerTask
should be fast in order to maintain the scheduling. In other words, it should not take seconds. In case your task has a lot of work to do, create a new thread within therun
method of your timertask.
– Robert Kock
Nov 20 '18 at 9:31
@Robert Kock ,thank you for your reply,I agree with your opinion.
– zhiwen zhang
Nov 21 '18 at 7:19
add a comment |
It doesn't answer your question but aTimerTask
should be fast in order to maintain the scheduling. In other words, it should not take seconds. In case your task has a lot of work to do, create a new thread within therun
method of your timertask.
– Robert Kock
Nov 20 '18 at 9:31
@Robert Kock ,thank you for your reply,I agree with your opinion.
– zhiwen zhang
Nov 21 '18 at 7:19
It doesn't answer your question but a
TimerTask
should be fast in order to maintain the scheduling. In other words, it should not take seconds. In case your task has a lot of work to do, create a new thread within the run
method of your timertask.– Robert Kock
Nov 20 '18 at 9:31
It doesn't answer your question but a
TimerTask
should be fast in order to maintain the scheduling. In other words, it should not take seconds. In case your task has a lot of work to do, create a new thread within the run
method of your timertask.– Robert Kock
Nov 20 '18 at 9:31
@Robert Kock ,thank you for your reply,I agree with your opinion.
– zhiwen zhang
Nov 21 '18 at 7:19
@Robert Kock ,thank you for your reply,I agree with your opinion.
– zhiwen zhang
Nov 21 '18 at 7:19
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388353%2fhow-to-understand-catch-up-of-java-timer-scheduleatfixedrate-method-and-sc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53388353%2fhow-to-understand-catch-up-of-java-timer-scheduleatfixedrate-method-and-sc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It doesn't answer your question but a
TimerTask
should be fast in order to maintain the scheduling. In other words, it should not take seconds. In case your task has a lot of work to do, create a new thread within therun
method of your timertask.– Robert Kock
Nov 20 '18 at 9:31
@Robert Kock ,thank you for your reply,I agree with your opinion.
– zhiwen zhang
Nov 21 '18 at 7:19