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 7983433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:57:23+00:00 2026-06-04T10:57:23+00:00

How anyone can write an Indefinite running Thread without a costly loop like while?

  • 0

How anyone can write an Indefinite running Thread without a costly loop like while?

Like I don’t want something like this

public void run() {

            while(true)
            {
                do Blah Blah Blah
                Thread.sleep(.....);

            }

        }

Any optimized way to write such a long running thread.

Thanks in advance.

  • 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-04T10:57:24+00:00Added an answer on June 4, 2026 at 10:57 am

    In what way do you consider while(true) “costly”? Do you have any evidence that this is affecting your performance? What operation would you consider to be cheaper than it?

    Note that from your comment:

    I guess such a infinite while loop blocks your CPU. Isn’t dat? So I consider it costly.

    it sounds like you’re considering the whole loop, not the while part. So let’s look at what you’re doing in the loop:

    do Blah Blah Blah
    Thread.sleep(.....);
    

    You’re explicitly telling the thread to sleep. While it’s sleeping, it won’t be consuming CPU resources. So no, it’s not costly. Admittedly if your sleep period is very short and your “do Blah Blah Blah” is very quickly, you’ll be looping an awful lot – but in that case you should consider increasing the sleep time. (You should also consider other approaches which allow you to signal to the thread that you wish it to quit cleanly, and do so even while it’s sleeping, without having to interrupt the thread.)

    You haven’t given us any information about what you’re really trying to achieve, but fundamentally the use of while isn’t a problem here. Just think about how often you want to loop, and whether it’s really a simple time-based value. For example, if this thread is meant to process work, then perhaps you want a producer/consumer queue of some description – there’s no need for the thread to sleep while it has work to do, but there’s no need for it to wake up until there’s work to do. That’s only an example, of course – without more information we really can’t give much more advice.

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

Sidebar

Related Questions

I want to learn to write a thread pool in Java Can anyone point
Anyone familiar with a way how can I can i write something to the
anyone can explain me, why this parts of code are acting differently? while((c =
I'm in over my head on this one. Can anyone write an if statement
Can anyone tell me how to write and run msbuild by using VS 2005?
can anyone help me write a linq query, its a bit confusing... Basically i
Can anyone tell me how to write javascript code that removes everything after the
Can anyone please tell me how to write a small div container in HTML
Can anyone help me debug this program? The following is server code : package
Anyone know how can I write the following update code by using the BLToolkit

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.