Question

For the life of me, I cannot figure out why this program does not work in Java 7. I've run it with no problems in using Java 6, but as soon as I run it with Java 7, it doesn't work.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class HelloWorld implements ActionListener {

JButton button;
boolean state;

public HelloWorld(){
    init();
    state = false;
    System.out.println("state - "+state);

    while (true){
        if (state == true){
            System.out.println("Success");
        }
    }
}

private void init(){
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    button = new JButton("Button");
    button.addActionListener(this);
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    JButton source = (JButton)e.getSource();

    if (source == button){
        state = !state;
        System.out.println("state - "+state);
    }
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    new HelloWorld();
}

}

Using Java 6, if I press the button, it will print out the phrase "Success" until I hit the button again. Using Java 7 registers that the button was pressed and the value of state was changed to true, but the phrase "Success" is never printed. What's going on?

Était-ce utile?

La solution

Add volatile to the field declaration.

Without volatile, changes in the field are not guaranteed to be visible on other threads.
In particular, the JITter is free to believe that the field never changes on the main thread, allowing it to remove the if entirely.

Autres conseils

When you show the JFrame

 frame.setVisible(true); 

The Java Show the window and stop the execution on this line.

You configured the window to exit on close:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This program will terminate after you close the window.

So the code after the init() call will never be executed.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top