2

This is a part of my program.

Thread rcv_thread = new Thread(new Runnable() {

@Override
public void run() {
    // TODO Auto-generated method stub
    System.out.println("getmsg");
    while (true) {
        /*try {
            Thread.sleep(0);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        if (!getmsg.isEmpty() && socket.isConnected()) {
            System.out.println("handle");
            getmsg_ = getmsg.get(0).split(";");
            if (socket.getInetAddress().toString()
                .equals("/" + getmsg_[0])) {
            PW.println(getmsg_[2] + " sent from"
                + getmsg_[1]);
            getmsg.remove(0);
            }
        }
    }
}
});         
rcv_thread.start();

It is really so strange.The program never runs in the range of while(true){} while I comment the range of try{}catch{} But if I cancel the comment in the range of try{}catch{} The loop will successfully runs and runs in the range of while(true){}

This is my problem. Sorry for my poor English.

1 Answers1

0

Without wait() (which is better replacement for sleep() here) your loop will consume ALL CPU resources and as such the content maybe disabled by the compiler.
The actual reason in your case will vary depending on the architecture/Java compiler/JVM/settings...

Germann Arlington
  • 3,315
  • 2
  • 17
  • 19
  • So if I need to run an unlimited loop like `While(true){}` in Thread , I need to add `sleep`() ,right?? – facebook-100000295003008 May 04 '14 at 11:23
  • As I said - `wait()` is better than `sleep()` in this content. But the answer is yes. Otherwise your code will consume all available resources and will kill your system. – Germann Arlington May 04 '14 at 11:26
  • But why I just added `System.out.println("got");` after `while(true)` ,the loop will be running?? Why?? – facebook-100000295003008 May 04 '14 at 11:29
  • Are you saying that your code with `System.out.println("got");` inside the loop **is** running and producing (a lot of) "got" output lines? In your question you said that it does **not** produce the output without `sleep()` statement? – Germann Arlington May 04 '14 at 11:34
  • After that,I add `System.out.println("get")` instead of `sleep`.But it runs – facebook-100000295003008 May 04 '14 at 12:10
  • I mean if I add nothing in the `while(true){}` like I posted,the loop won't run. But I just added `System.out.println("get")` or `sleep()`,it runs. Why?? – facebook-100000295003008 May 04 '14 at 12:39
  • How do you know that the loop does not run? It will only produce the output if your conditions `if (!getmsg.isEmpty() && socket.isConnected()) {` are met. May be the data is not coming but the loop is running and you can't see it? As it is your loop will consume all you system resources and it may stall other threads that are expected to produce the data. – Germann Arlington May 04 '14 at 13:49
  • I just compared to two results under the `if (!getmsg.isEmpty() && socket.isConnected())` in `true` condition. But if I didn't add `System.out.println("get")`,there is no `System.out.println("handle")` shown in the monitor. If I added `System.out.println("get")` , `System.out.println("handle")` would show in the monitor.Thanks for responding. – facebook-100000295003008 May 04 '14 at 14:23