How can I execute a task at regular intervals, but in a way so a step can't start until the last one has finishedWait until any of Future<T> is doneHow can I create an executable JAR with dependencies using Maven?Blackberry stopwatch implementationjava swing clear the event queuejava Timer TimerTask multiple threadsI need help understanding the scheduleAtFixedRate method of theTimer class in javaStrange java behavior of wait/notifyConcurrent collection of listenerscrawler4j not working while using it with TimerTaskUnexpected behaviour with Java timer task
What is the closest airport to the center of the city it serves?
How can I get people to remember my character's gender?
How to draw a stack in drawstack/TiKz?
In "Avengers: Endgame", what does this name refer to?
Is there an age requirement to play in Adventurers League?
When an imagined world resembles or has similarities with a famous world
How to preserve a rare version of a book?
Why does sound not move through a wall?
Is there precedent or are there procedures for a US president refusing to concede to an electoral defeat?
Why didn't this character get a funeral at the end of Avengers: Endgame?
Is the book wrong about the Nyquist Sampling Criterion?
What Kind of Wooden Beam is this
Which US defense organization would respond to an invasion like this?
Can I hide the part of long lines that exceeds the visual line?
How do I, as a DM, handle a party that decides to set up an ambush in a dungeon?
Drawing an hexagonal cone in TikZ 2D
おはよう written as おはよ?
Motion-trail-like lines
My first C++ game (snake console game)
Would a small hole in a Faraday cage drastically reduce its effectiveness at blocking interference?
Page count conversion from single to double-space for submissions
Could a brown dwarf in our outer solar system remain undetected today?
Counting the Number of Real Roots of A Polynomial
Is there a word that describes the unjustified use of a more complex word?
How can I execute a task at regular intervals, but in a way so a step can't start until the last one has finished
Wait until any of Future<T> is doneHow can I create an executable JAR with dependencies using Maven?Blackberry stopwatch implementationjava swing clear the event queuejava Timer TimerTask multiple threadsI need help understanding the scheduleAtFixedRate method of theTimer class in javaStrange java behavior of wait/notifyConcurrent collection of listenerscrawler4j not working while using it with TimerTaskUnexpected behaviour with Java timer task
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an agent-based model that I need to update every t milliseconds. In each update step, all the agent objects need to be notified and execute some code. I want every step to last t or until the last step has finished, whatever is longer.
How can I do this? I'm trying to use Timer.schedule, but it doesn't seem to be waiting because I'm getting a ConcurrentModificationException.
public void startClock(long delay)
Timer timer = new Timer("clock", true);
TimerTask clockTask = new TimerTask()
@Override
public void run()
World.INSTANCE.update(); //this updates all agents
step++;
for (Timed task : listeners)
task.run();
;
timer.schedule(clockTask, delay, interval); //miliseconds
public void startClock()
startClock(interval);
java concurrency
add a comment |
I have an agent-based model that I need to update every t milliseconds. In each update step, all the agent objects need to be notified and execute some code. I want every step to last t or until the last step has finished, whatever is longer.
How can I do this? I'm trying to use Timer.schedule, but it doesn't seem to be waiting because I'm getting a ConcurrentModificationException.
public void startClock(long delay)
Timer timer = new Timer("clock", true);
TimerTask clockTask = new TimerTask()
@Override
public void run()
World.INSTANCE.update(); //this updates all agents
step++;
for (Timed task : listeners)
task.run();
;
timer.schedule(clockTask, delay, interval); //miliseconds
public void startClock()
startClock(interval);
java concurrency
Can't you just have a boolean member variable that you check before executing the code in run()?
– chips
Mar 23 at 3:05
add a comment |
I have an agent-based model that I need to update every t milliseconds. In each update step, all the agent objects need to be notified and execute some code. I want every step to last t or until the last step has finished, whatever is longer.
How can I do this? I'm trying to use Timer.schedule, but it doesn't seem to be waiting because I'm getting a ConcurrentModificationException.
public void startClock(long delay)
Timer timer = new Timer("clock", true);
TimerTask clockTask = new TimerTask()
@Override
public void run()
World.INSTANCE.update(); //this updates all agents
step++;
for (Timed task : listeners)
task.run();
;
timer.schedule(clockTask, delay, interval); //miliseconds
public void startClock()
startClock(interval);
java concurrency
I have an agent-based model that I need to update every t milliseconds. In each update step, all the agent objects need to be notified and execute some code. I want every step to last t or until the last step has finished, whatever is longer.
How can I do this? I'm trying to use Timer.schedule, but it doesn't seem to be waiting because I'm getting a ConcurrentModificationException.
public void startClock(long delay)
Timer timer = new Timer("clock", true);
TimerTask clockTask = new TimerTask()
@Override
public void run()
World.INSTANCE.update(); //this updates all agents
step++;
for (Timed task : listeners)
task.run();
;
timer.schedule(clockTask, delay, interval); //miliseconds
public void startClock()
startClock(interval);
java concurrency
java concurrency
edited Mar 23 at 4:10
FailingCoder
321110
321110
asked Mar 23 at 2:46
Pedro VictoriPedro Victori
86
86
Can't you just have a boolean member variable that you check before executing the code in run()?
– chips
Mar 23 at 3:05
add a comment |
Can't you just have a boolean member variable that you check before executing the code in run()?
– chips
Mar 23 at 3:05
Can't you just have a boolean member variable that you check before executing the code in run()?
– chips
Mar 23 at 3:05
Can't you just have a boolean member variable that you check before executing the code in run()?
– chips
Mar 23 at 3:05
add a comment |
2 Answers
2
active
oldest
votes
Take a look at ScheduledExecutorService#scheduleAtFixedRate(). It will execute your task either every t milliseconds or immediately after the last execution if it took longer than t.
add a comment |
You can also evaluate Akka Scheduler. It ensures that there will be no overlap of executions of the runnable. Here's the snippet from their documentation-
If the execution of the runnable takes longer than the interval, the subsequent execution will start immediately after the prior one completes (there will be no overlap of executions of the runnable). In such cases, the actual execution interval will differ from the interval passed.
add a comment |
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%2f55310126%2fhow-can-i-execute-a-task-at-regular-intervals-but-in-a-way-so-a-step-cant-star%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Take a look at ScheduledExecutorService#scheduleAtFixedRate(). It will execute your task either every t milliseconds or immediately after the last execution if it took longer than t.
add a comment |
Take a look at ScheduledExecutorService#scheduleAtFixedRate(). It will execute your task either every t milliseconds or immediately after the last execution if it took longer than t.
add a comment |
Take a look at ScheduledExecutorService#scheduleAtFixedRate(). It will execute your task either every t milliseconds or immediately after the last execution if it took longer than t.
Take a look at ScheduledExecutorService#scheduleAtFixedRate(). It will execute your task either every t milliseconds or immediately after the last execution if it took longer than t.
answered Mar 23 at 8:58
Stefan FerstlStefan Ferstl
4,05131933
4,05131933
add a comment |
add a comment |
You can also evaluate Akka Scheduler. It ensures that there will be no overlap of executions of the runnable. Here's the snippet from their documentation-
If the execution of the runnable takes longer than the interval, the subsequent execution will start immediately after the prior one completes (there will be no overlap of executions of the runnable). In such cases, the actual execution interval will differ from the interval passed.
add a comment |
You can also evaluate Akka Scheduler. It ensures that there will be no overlap of executions of the runnable. Here's the snippet from their documentation-
If the execution of the runnable takes longer than the interval, the subsequent execution will start immediately after the prior one completes (there will be no overlap of executions of the runnable). In such cases, the actual execution interval will differ from the interval passed.
add a comment |
You can also evaluate Akka Scheduler. It ensures that there will be no overlap of executions of the runnable. Here's the snippet from their documentation-
If the execution of the runnable takes longer than the interval, the subsequent execution will start immediately after the prior one completes (there will be no overlap of executions of the runnable). In such cases, the actual execution interval will differ from the interval passed.
You can also evaluate Akka Scheduler. It ensures that there will be no overlap of executions of the runnable. Here's the snippet from their documentation-
If the execution of the runnable takes longer than the interval, the subsequent execution will start immediately after the prior one completes (there will be no overlap of executions of the runnable). In such cases, the actual execution interval will differ from the interval passed.
answered Mar 23 at 16:05
DollygDollyg
355
355
add a comment |
add a comment |
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%2f55310126%2fhow-can-i-execute-a-task-at-regular-intervals-but-in-a-way-so-a-step-cant-star%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
Can't you just have a boolean member variable that you check before executing the code in run()?
– chips
Mar 23 at 3:05