Java Q&A 
by Joshua Fox

Listing One
public void run() {
   try {
       for(;;) {
         output_stream.send(dummy_message);
       }
    } catch (IOException ioe){ 
        socket_and_remote_resources.close();
    }
}

Listing Two
private boolean message_just_received = false;
public void run() {
    for(;;) {
       try {
       // This wait/notify technique is 
           // preferable to a loop that repeatedly checks the situation.
           synchronized (this) {
                  wait(TIME_BETWEEN_ACTIVE_PROBES * 2);
           }
           if (!message_just_received)
                  output_stream.send(dummy_message);
           }
           message_just_received = false;
       } catch (InterruptedException ie) {
           break;
       } catch (IOException ioe) {
           socket_and_remote_resources.close();
           break;
       }
   }
}
// callback, called when any message is received
synchronized void on_message_received(String s) { 
   message_just_received = true;
      notify();
}

Listing Three
public void run() {
     while (!endRequested()) {
           if (remote_side_has_been_silent_for_too_long()) {
                output_stream.send(are_you_there);
     }
     if (remote_side_been_silent_for_WAY_too_long ()) {
            socket_and_remote_resources.close();
     }
     sleep(1000L);
   }
}
    
Listing Four
// callback, called when "Are you there" is received
void on_are_you_there_received() { // callback 
     try {
        // I-Hear-You does not require a special message type
        output_stream.send(dummy_message); 
     } catch (IOException ioe){ 
        socket_and_remote_resources.close();
     }
}

Listing Five
public void run() {
    for(;;) {
            try {
                  output_stream.send(are_you_there_message);
            } catch (IOException ioe) {
                  socket_and_remote_resources.close();
                  break; 
            }
            sleep(TIME_BETWEEN_ACTIVE_PROBES);
            if (remote_side_has_been_silent_for_too_long()) {
                  socket_and_remote_resources.close();
            }
     }
}

Listing Six
public void run() {
     for(;;) {
           // Can be implemented alternately using wait/notify.
           // See PassiveKeepAlive (Listing Two).
           sleep(TIME_BETWEEN_ACTIVE_PROBES);
           if (remote_side_has_been_silent_for_too_long()) {
                 socket_and_remote_resources.close();
           }
      }
}
// callback, called when "Are you there" is received
void on_are_you_there_received() { 
      try {
           output_stream.send(dummy_message); 
      } catch (IOException ioe){ 
           socket_and_remote_resources.close();
      }
}

Listing Seven
public void run() {
     for(;;){
            try {
                 output_stream.send(dummy_message); 
            } catch (IOException ioe) {
                 socket_and_remote_resources.close();
                 break;
            }
            if (remote_side_has_been_silent_for_too_long()) {
                 output_stream.send(are_you_there);
            }
            if (remote_side_has_been_silent_for_WAY_too_long ()) {
                 socket_and_remote_resources.close();
            }
            sleep(TIME_BETWEEN_ACTIVE_PROBES);
    }
}

Listing Eight
public void run() {
     for(;;) {
            try {
                if(remote_side_has_been_silent_for_too_long() ){
                      output_stream.send(dummy_message); 
                }
            } catch (IOException ioe) {
                getSender().close();
            }
            sleep(TIME_BETWEEN_ACTIVE_PROBES/2);
      }
}
// callback, called when "Are you there" is received
void on_are_you_there_received () { 
     try {
          // I-Hear-You does not require a special message type
          output_stream.send(dummy_message); 
     } catch (IOException ioe){ 
          socket_and_remote_resources.close();
     }
}  





1


