How to avoid being busy in Java

I have a multi-threaded application where a thread is sending a message to another thread. The pending thread polls for a message and responds (locks are being processed). Like this:

Wait sequence code:

while(true)
{
  if(helloArrived())
    System.out.println("Got hello");
  if(byeArrived())
    System.out.println("Got bye");
  if(stopArrived())
    break;
}

      

I want to avoid this cpu hogging technique and use something else instead. Any ideas?

Edit: the actual code is below:

BlockingQueue<Mail> killMeMailbox = new LinkedBlockingQueue<Mail>();
BlockingQueue<Mail> messageMailbox = new LinkedBlockingQueue<Mail>();

public void run()
    {
        while(true)
        {
            if(killMeMailbox.size() > 0)
            {
                break;
            }
            if(messageMailbox.size() > 0)
            {
              System.out.println(messageMailbox.poll());
            }
        }
     }

public void receiveMail(Mail mail)
    {
        //kill
        if(mail.from == -1)
        {
            killMeMailbox.add(0);
        }
        else
        {
            //other
            try
            {
                messageMailbox.put(mail);
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
    }

      

+3


source to share


4 answers


The correct way to avoid this is to use the wait / notify mechanism implemented java.lang.Object

, or one of the higher level concurrency mechanisms provided by the Java class libraries:

(Choose the mechanism that best suits your specific use case)


Using is Thread.sleep

not a good solution. While you reduce the CPU load (versus polling cycle), the flip side is that you reduce responsiveness.




I am now using BlockingQueue. But maybe I am doing it wrong. I just added the code above. Do you see my problem?

Yes. You are using the queue in a way that avoids blocking. This is the wrong approach. You should use take()

(which will block until the entry is available) instead poll()

, and get rid of the code that checks the queue size.

Your "killMeMailbox" stuff seems to be for you to stop waiting for mail. You must implement this with Thread.interrupt

. (Interrupt unblocks call take()

...)

+3


source


You make an expectation of expectation . You should never do this because it wastes CPU cycles. A thread or process waiting for an event must be in a blocked state . Possible ways to achieve this are:



+2


source


If you can reformulate your problem from the perspective of a task executor, consider using a SingleThreadExecutor . If you need something a little more exotic - a parallel queue or even wait () / notify () .

0


source


Have you tried putting some Thread.sleep to avoid the continuous while loop? It will free up the processor for your other threads and avoid the pigs

http://docs.oracle.com/javase/tutorial/essential/concurrency/sleep.html

-1


source







All Articles