Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 717919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:28:02+00:00 2026-05-14T05:28:02+00:00

If I set a timer to execute a code every 3 seconds. If the

  • 0

If I set a timer to execute a code every 3 seconds. If the code isn’t finished in 3 seconds, what will happen? The computer will terminal the code or wait for the code finish or continue the timer, and execute the code with the unfinished code concurrently.

If the computer will execute the code with the unfinished code concurrently, what happen if a variable involve in the method. For example, the first line of run may doing i–, but last line is doing i++. If it run concurrently, when the unfinished code is still running, but a new running cycle is begin, the i value is being added by a new running cycle, so when the pervious cycle run to the last line, will the i value do wrong(because the new running cycle is doing i–, before the pervious code is finished). If yes, how to avoid it?

int delay = 0;   // delay for 0 sec.
int period = 3000;  // repeat 3 sec.
int i = 0;
Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
            i--;
            // Task here ...
            // It may take more than 3 sec to finish, what will happen?
            i++;
        }
    }, delay, period);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:28:02+00:00Added an answer on May 14, 2026 at 5:28 am

    Each Timer only uses one thread to service its tasks. The timer thread will run your task until it completes, and only then try to schedule its next execution. The documentation for Timer acknowledges this problem and warns its users against tasks “bunching up”.

    Once your task completes, the governing Timer will then attempt to schedule it again. The next scheduled execution will likely be in the past, per Timer$TimerThread#mainLoop()‘s rescheduling policy: It adds the task’s period to the last planned execution time of the task. Since in your case that planned execution time will be in the past by more than three seconds, adding three seconds to it still yields a next planned execution time in the past.

    That’s fine; the algorithm accommodates such slippage. Your task will run again immediately after the last run completes. What you won’t get is your desired once-every-three-seconds behavior. Instead, you’ll get as-often-as-possible-but-no-more-frequent-than-every-three-seconds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.