views:

318

answers:

4

I have something like this:

for(int i=0; i<5; i++){
    mytextarea.setText("hello " + i);
    try{
        Thread.currentThread().sleep(1000); //to give time for users to read
    } catch(Exception e){}
}

I'm expecting it will display "hello 0" in the text area, wait for 1 second, then display "hello 1", then wait for 1 second, etc.

But what happen is different, it waits for 5 seconds, then it displays "hello 4".

Any idea?

+10  A: 

Yes - you're basically blocking the UI thread, so it's never getting round to actually updating.

Sleeping in a UI thread is a really bad idea.

If you want to do something like this, you should use a Timer. (I'm assuming you're using Swing. If not, please edit your question to indicate which UI framework you're using.)

You should also note that Thread.sleep is a static method. You're using it as if it were an instance method. Admittedly you happen to be calling it "on" the current thread, but your usage suggest that you think that:

Thread t = new Thread(...);
t.start();
t.sleep(1000);

would make the new thread sleep. It wouldn't - it would make the current thread sleep, because that's what Thread.sleep always does. IMO it was a mistake for Java to allow you to call static methods in this way - if you're using Eclipse, there's an option to make this a warning or error.

Jon Skeet
+2  A: 

While you code is waiting no events will be processed

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

Read javadoc of javax.swing.SwingUtilities.invokeAndWait() and invokeLater() this may help

EDIT: Thanks to Jon and Samuel puting all ideas together:

public class Swing extends JPanel {
    JTextField textField;
    static JTextArea textArea;
    static int line = 1;

    public Swing() {
        super(new BorderLayout());
        textArea = new JTextArea(5, 20);
        add(textArea);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("TextDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Swing());
        frame.pack();
        frame.setVisible(true);

        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                textArea.append("Hello " + line++ + "\n");
            }
        };
        if (line < 5) {
            new Timer(1000, taskPerformer).start();
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
stacker
+2  A: 

Like explained in Jon Skeet's answer, you should use a timer since you can't block the EDT and expect UI updates. Below is your sample snippet rewritten to use a Swing timer.

ActionListener action = new ActionListener() {
    int i = 0;

    public void actionPerfomed(ActionEvent e) {
        mytextarea.setText("hello " + i++);
    }
};
new javax.swing.Timer(1000, action).start();

See How to Use Swing Timers in the Swing Tutorial for more information about timer capabilities.

Samuel Sjöberg
+1  A: 

Another way to not block the Event Dispatch Thread (EDT) is to start a new Thread:

Thread thread = new Thread(new Runnable() {
    @Override
    public void runt() {
        for (int i=0; i<5; i++) {
            mytextarea.setText("hello " + i);
            try {
                Thread.sleep(1000); //to give time for users to read
            } catch (InterruptedException e) {
                break;    // interrupt the for
            }
        }
    }
});
thread.start();

EDIT:
In general Swing is NOT thread safe, that is, Swing methods not marked as thread safe should only be called on the EDT. setText() is thread-safe, so it's no problem in the above code.

To run a code on the EDT, you use invokeAndWait() or invokeLater() from javax.swing.SwingUtilities (or from java.awt.EventQueue).

For more details see: Swing's Threading Policy

Carlos Heuberger
Be careful to only call thread safe methods with this approach. JTextComponent.setText is thread safe, although most Swing methods are not.
Samuel Sjöberg
good point: added it to the answer. Thanks.
Carlos Heuberger