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

  • SEARCH
  • Home
  • 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 9313409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:52:31+00:00 2026-06-19T01:52:31+00:00

I need to have a background thread that constantly does an action, sleep for

  • 0

I need to have a background thread that constantly does an action, sleep for X seconds and do the action etc.
Basically the run method is something like:

while(!isInterrupted()){  
   //do something  
   Thread.sleep(10);  
}  

My question is:

  1. Does it make sense to use an executor in this case? Since I am not
    spawning threads, is even in this case using an executor (single
    threaded) better?
  2. Additionally if I want a guarantee that the thread goes in the do
    something
    part in exactly 10 seconds, is that possible via using
    just a custom thread or more guaranteed via an executor? I mean if I have a hard limit of 10 seconds to perform an action, what can I do to achieve it? I assume that the time that the code goes back in do something may fluctuate due to scheduling etc. How could I get such a guarantee?
  • 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-06-19T01:52:33+00:00Added an answer on June 19, 2026 at 1:52 am

    If you are using only a single thread which is a forever running task like yours then you can use your present logic.

    But only when you have some small tasks that need to be run, then there is point in using SingleThreadPool.

    How could I get such a guarantee?

    There is no such guarantee from the OS side (Linux or Windows), that the thread will return from sleep at exact 10 seconds. Try increasing thread priority, but that too is not guaranteed to work.

    Your logic should not be dependent on such hard timings IMO.

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

Sidebar

Related Questions

I have numerous functions that are called from a background thread, but then need
I have a process that one of its background threads ( Thread-A ) does
I need to have a background process that runs independent of my app and
I need to have ALWAYS a background service that will synchronize my Android application
I need to animate my background which have 6 images and that needed to
if i have a php background job, may need to run 4-5hours or more
I need change background of all text that have two spaces from the start
I have a block of text that I need to give a background colour
We have a background thread that needs to do some fetching.. but it doesnt
Basically, this is what happens. I have a thread(endless loop) that runs as a

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.