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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:35:21+00:00 2026-05-31T08:35:21+00:00

I have a main loop in my thread, and part of it tests for

  • 0

I have a main loop in my thread, and part of it tests for if an idle boolean is true. If it is, it will call Thread.sleep(1) upon each iteration of the loop. Is this an efficient way to go about doing this? My goal is for the thread to take minimal CPU usage when idle.

  • 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-31T08:35:22+00:00Added an answer on May 31, 2026 at 8:35 am

    No. Use Object.wait instead and make sure you synchronize on the object containing the boolean. If you don’t synchronize and the boolean is not volatile, you have no memory barrier so there is no guarantee that the polling thread will see the change to the boolean.

    According to the javadoc:

    This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

    • Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
    • Some other thread invokes the notifyAll method for this object.
    • Some other thread interrupts thread T.
    • …

    so the thread will take no CPU while it is waiting to be notified.

    The code below is a simple idle flag with a waitUntilIdle method that your main method can call and a setIdle method that can be called by another thread.

    public class IdleFlag {
      private boolean idle;
    
      public void waitUntilIdle() throws InterruptedException {
        synchronized (this) {
          while (true) {
            // If the flag is set, we're done.
            if (this.idle) { break; }
            // Go to sleep until another thread notifies us.
            this.wait();
          }
        }
      }
    
      public void setIdle() {
        synchronized (this) {
          this.idle = true;
          // Causes all waiters to wake up.
          this.notifyAll();
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main loop which is fully data-driven: it has a blocking call
On Linux under X11 and using GTK+ you have something called Main Loop. Once
I have my main GUI thread, and a second thread running inside it's own
I have one 'main' thread that creates an array of objects of class SlowData
I have a main() , which spawns a thread, and then joins to it.
I have a Main method that creates a message loop called SysTrayApp: void Main()
I have a simple iPhone game consisting of two threads: the main game loop
I have a main loop, and there are some loops inside it. I want
I have a Quartz context declared in the main loop as UIGraphicsBeginImageContext(mySize); ctx =
I have one main thread, and many other background threads. The main usage of

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.