0

I have this code executed as a thread for each client to detect the disconnection of an user in my server:

private void detectDisconnect (Client user)
{ 
    try  
    {
        boolean listening = true;

        while (listening)
        { 
            System.out.println("<" + user.getUserName() + "> Sleeping...");
            user.setDisconnected(true); 
            send(user, "heartBeat#"); 
            Thread.sleep(Main.LATENCY);
            System.out.println("<" + user.getUserName() + "> I wake up!");

            if (user.isDisconnected()) { listening= false; exitClient(user);                 
        }  
    } 
    catch (InterruptedException e) { e.printStackTrace(); }
}

The server before of sleeping put user.setDisconnected(true); and if the client send a response before of waking up I change to user.setDisconnected(false); in other method(), so this thread won't exit the Client unless in the latency's period doesn't get any message... This works fine, but sometimes randomly I could check that after the message of "Sleeping..." does not appear the message "I wake up!" (!!!), and the worst thing is that ALL MY PROGRAM GETS FROZEN, so the server doesn't listen or send messages to any client anymore :( even being these in separated Threads executing!....

I don't know why this happen, but I think that all the problems are caused by some eventually fatal error in Thread.sleep(). Could be possible? But I don't receiving any error message in the catch(Exception e){}, so I'm going crazy xD

Help please!!

Andrés Fg
  • 138
  • 1
  • 7

0 Answers0