Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange java behavior with while loop and queue

I have strange behavior of while(true) loop. Here is the code:

As a member of class I have:

static Queue<Object> response = new LinkedList<Object>();

... and a function:

private void read() {

    while (true)
    {
        System.out.println("foo");
        if(!(response.isEmpty()))
        {

            if((Boolean)response.peek() == true)
            {
                view.dispose();
                LogInControler controler= new LogInControler();
                disableMasterLogin();
                response.poll();
                return;
            }
            else if((Boolean)response.poll() == false)
            {
                JOptionPane.showMessageDialog(view.getRootPane(), 
                        "Wrong username or password.");
                view.tfUsername.requestFocus();
                return;
            }
        }
    }
}

When object is received from server(via Socket), InputController class pass that object to appropriate controller, in this case MasterLogInController and put it in Queue response. I am waiting for that response in while(true) loop, but the problem is if I remove "System.out.printline("foo");" loop will be entered only once!? With this syso line I "force" while loop to do loops until response is received. What's wrong here?

like image 205
maleta Avatar asked Dec 09 '12 23:12

maleta


People also ask

What's wrong with while loop?

Actually it is an infinite loop. while (r == false) ; is an infinite loop doing nothing, since r is initialized to false. Stop trying to put multiple things on a line. As much as anything, this is what caused your problem.

Which is better while or for loop in Java?

Use a for loop when you know the loop should execute n times. Use a while loop for reading a file into a variable. Use a while loop when asking for user input. Use a while loop when the increment value is nonstandard.

How do you stop a while loop when a condition is met Java?

Java Break Statement When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. The Java break statement is used to break loop or switch statement.

When should you use a while loop in Java?

The while loop can be thought of as a repeating if statement. While loop in Java comes into use when we need to repeatedly execute a block of statements. The while loop is considered as a repeating if statement. If the number of iterations is not fixed, it is recommended to use the while loop.


1 Answers

I assume you have several threads running.

System.out.println creates a memory barrier which probably helps your code see some variable which is otherwise not visible (because of lack of synchronization).

In particular, your queue is not thread safe and does seem to be safely published. So it is very conceivable that:

  • your while loop might see response as null ==> NullPointerException
  • reponse.isEmpty() might return false but response.peek() might return null, which you then cast to a Boolean and unbox in your condition if((Boolean)xxx == true) ==> NullPointerException
  • etc.

Apart from the sound advice given in the comments to help understand the cause, you should make the code thread safe. For example, you could use a thread safe BlockingQueue. But that will probably not be enough (because of the way your various if / if / else if statements are laid out and the fact that the queue might be changed by another thread between each of those statements).

like image 76
assylias Avatar answered Sep 30 '22 03:09

assylias