1

I have the following code:

while(!loggedIn) {
    loggedIn = login.getDone();
}
System.out.println("madeIt");

The print line never executes, even though I know login.getDone() returns true at some point. If, however, I use the following code:

while(!loggedIn) {
    loggedIn = login.getDone();
    System.out.println(loggedIn);
}
System.out.println("madeIt");

Then the last print line executes. I have no idea why. Does anyone have any insight into why this happens?

For more code, see the entirety of my main class:

public class GameManager {
    public static void main (String args[]) {
        boolean loggedIn = false;
        String username;
        int playerNum;
        int i = 0;
        Login login = new Login();
        while (!loggedIn){
            loggedIn = login.getDone();
            //System.out.println(loggedIn);                                                                                                                                                                 
        }
        login.close();
    System.out.println("Logged in");
    }
}

and the entirety of the login class:

import squint.*;
import javax.swing.*;
import java.util.*;

public class Login extends GUIManager {
    private final int WINDOW_WIDTH = 200, WINDOW_HEIGHT = 150;

    private JButton login;

    String name;
    boolean done;
    int playerNumber;

    public Login() {
        done = false;
        this.createWindow( WINDOW_WIDTH, WINDOW_HEIGHT );
        this.setTitle("Hearts Login");
    login = new JButton("Click");

        contentPane.add(login);
    } 

    public void buttonClicked( ) {
        done = true;
        System.out.println(done);
    }

    public boolean getDone() {
        return done;
    }

}

And squint can be found at http://dept.cs.williams.edu/~cs134/squintV2.20.jar

Cardsox425
  • 11
  • 3
  • maybe your program exits before you had a chance to see it ? – Mzf Mar 31 '14 at 22:47
  • 2
    Please include some more of this program for context, specifically the getDone() function. – Kyle Spencer Mar 31 '14 at 22:48
  • The getDone() function simply returns a global boolean variable in the Login. And the program doesn't exit before I have a chance to see it. There is more code after the print line and none of that executes either, the program just loops forever. – Cardsox425 Mar 31 '14 at 22:52
  • Are multiple threads involved? – Bohemian Mar 31 '14 at 22:56
  • No multiple threads. See edited question for more code. – Cardsox425 Mar 31 '14 at 23:36
  • 1
    You are using Swing UI which means that you have multiple threads even if you did not create a start a thread explicitly, because Swing will create a new event-dispatching thread automatically. So what you have here is a simple data visibility problem in a multi-threaded application. You can verify that assumption by making `done` variable volatile. – Oleg Estekhin Apr 01 '14 at 06:04
  • possible duplicate of [Loop doesn't see changed value without a print statement](http://stackoverflow.com/questions/25425130/loop-doesnt-see-changed-value-without-a-print-statement) – Boann Aug 23 '14 at 10:21

1 Answers1

2

This could happen if loggeIn or the variable that getDone returns is set to true by another thread and it is not declared volatile.

It may be easier to determine your problem if you give a little more code.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • @Cardsox425 - As Oleg says - you are using Swing so there are multiple threads happening behind the scenes. Try setting `done` to `volatile`. I think it will fix your problem. – OldCurmudgeon Apr 01 '14 at 07:53