下面的代码里,每次用{write(***)}向服务器发送指令的时候,要等到线程(run)接收到服务器返回的结果后,再发送下一条指令,所以我使用了线程和synchronized。现在用this.wait(6000)就能实现这个效果,但是等待的时间是我自己估算的,实际执行的时间有时长,有时短。请问有其他方法能知道run什么时候结束,然后就解除锁定吗?public class TmhTelnet1 implements Runnable, TelnetNotificationHandler {  
  public static void main(String args[]){  
    Thread th_read = new Thread(this);     
    th_read.start();
    write("username");
    write("password");
    write("ipconfig /all");
  }  private void write(String command) {   
    synchronized (this) {
      out.println(command);  
        out.flush();  
        this.wait(6000);
    }
  }  public void run() {   
        InputStream instr = tc.getInputStream();
do {
  ret_read = instr.read(buff);
  if (ret_read > 0) {
    String str = new String(buff, 0, ret_read,TER_CODING);
          }
} while (ret_read >= 0);
  }
}