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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:45:22+00:00 2026-06-05T13:45:22+00:00

I am trying to figure out how to eliminate a busy loop in a

  • 0

I am trying to figure out how to eliminate a busy loop in a thread I have in Java. The thread does nothing but wait for a callback from the Listener class. The thread class looks something like this:

class MyThread implements Runnable, MessageListener{
  private Listen Listener;

  public MyThread(){
    Listener = new Listener(this);
  }

  public void run(){
    while(true){}
  }

  public void messageReceived(Message m){
    //do stuff
  }
}

I have tried to make this code as simple as possible. The idea is that Listener is waiting to receive some data from a serial port and when a message is received the thread will do some processing on it. I would prefer to use some synchronized variables such as a BlockingQueue of Message but I can’t modify the Listener code. The issue, of course, is that the run loop eats up processor cycles. My questions:

If I wait or sleep in the run loop will the function call still work as expected? (I’m not sure how to test that this works 100% of the time).

Is there some better way to avoid this loop altogether?

  • 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-05T13:45:23+00:00Added an answer on June 5, 2026 at 1:45 pm

    Do something like this

    class MyThread implements Runnable, MessageListener{
              private Listen Listener;
              private Object lock = new Object();
          public MyThread(){
            Listener = new Listener(this);
          }
    
          public void run(){
            while(true){
                synchronized (lock) {
                    try{
                        lock.wait();
                        // use the updated status
                    }catch (Exception e) {
                        e.printStackTrace()
                    }
                }
            }
          }
    
          public void messageReceived(Message m){
              synchronized (lock) {
                    try{
                        // Do something with the message here like update some status
                        lock.notify();                      
                    }catch (Exception e) {
                        e.printStackTrace()
                    }
    
          }
        }
    

    Once you get the event, you update some status/ store the message and release the notifying Thread. Then from your thread process the event

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

Sidebar

Related Questions

trying to figure out why this is happening - I have an input text
Trying to figure out why my silverlight app suddenly just displays nothing (right click
Trying to figure out the best way to have a templated brochure that creates
I have a problem and Google hasn't helped me much. I'm trying figure out
I have an 'inlineformset_factory', and I'm trying figure out how to set attributes for
I am trying to figure out a neat way to remove unused factors from
I have been going nuts trying to figure this out. I am trying to
I am trying to figure out the pattern that will get words from a
Trying to figure out how to open the default browser from a click on
I am trying to figure out how to eliminate line breaks when using add-content

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.