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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:12:19+00:00 2026-06-15T18:12:19+00:00

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

  • 0

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?

  • 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-15T18:12:20+00:00Added an answer on June 15, 2026 at 6:12 pm

    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).

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

Sidebar

Related Questions

I have strange behavior with android.R.layout.simple_dropdown_item_1line while using MultiAutoCompleteTextView . The code I use
I've found a strange behavior while using a reference variable. Here is class implementations:
I have realy strange behavior. I have this code (I remove unrelated code). $(function()
I have a very strange behavior with Request.Form . Here are two IIS 7
I have strange discarding behavior of struts2 while setting cache option for my image.
I have not programmed C++ for a while and encountered a strange behavior when
I have noticed strange behavior. While this one runs as expected: @a = ['0','1','2','3','4','5']
I have a very strange behavior on an application. The server is in New
I have this extremely strange behavior coming : In the below code: If I
I have encountered a strange behavior while executing an sql query on the Oracle

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.