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;








1















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);










share|improve this question
























  • Can't you just have a boolean member variable that you check before executing the code in run()?

    – chips
    Mar 23 at 3:05

















1















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);










share|improve this question
























  • Can't you just have a boolean member variable that you check before executing the code in run()?

    – chips
    Mar 23 at 3:05













1












1








1








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);










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












2 Answers
2






active

oldest

votes


















0














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.






share|improve this answer






























    0














    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.







    share|improve this answer























      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
      );



      );













      draft saved

      draft discarded


















      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









      0














      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.






      share|improve this answer



























        0














        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.






        share|improve this answer

























          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 8:58









          Stefan FerstlStefan Ferstl

          4,05131933




          4,05131933























              0














              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.







              share|improve this answer



























                0














                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.







                share|improve this answer

























                  0












                  0








                  0







                  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.







                  share|improve this answer













                  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.








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 23 at 16:05









                  DollygDollyg

                  355




                  355



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                      155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해