Why does this break statement break not work?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-04-19T12:53:28Z Indexed on 2010/04/19 13:03 UTC
Read the original article Hit count: 355

Filed under:
|
|

I have the following code:

public void post(String message) {
    final String mess = message;
    (new Thread() {
        public void run() {
            while (true) {
                try {
                    if (status.equals("serviceResolved")) {
                        output.println(mess);
                        Game.log.fine("The following message was successfully sent: " + mess);
                        break;
                    } else {
                        try {Thread.sleep(1000);} catch (InterruptedException ie) {}
                    }
                } catch (NullPointerException e) {
                    try {Thread.sleep(1000);} catch (InterruptedException ie) {}
                }
            }
        }
    }).start();
}

In my log file I find a lot of lines like this:

The following message was successfully sent: blablabla
The following message was successfully sent: blablabla
The following message was successfully sent: blablabla
The following message was successfully sent: blablabla

And my program is not responding.

It seems to me that the break command does not work. What can be a possible reason for that.

The interesting thing is that it happens not all the time. Sometimes my program works fine, sometimes the above described problem happens.

© Stack Overflow or respective owner

Related posts about java

Related posts about break