这个问题的一般解决方案较难,比如:
 - 该通讯线程正在等待于read(...)方法(在某些版本的JDK中存在这个问题)
 - 该通讯线程已取得某一个信号量,进入一个synchronized{...}块,但尚未
   出来,并且依然hold着这个信号量
 - 这时从外边可能无法停止这个通讯线程。如果在编程中考虑到这些问题,则在特定的情况下,针对这个优化过的通讯线程,
在某几种JVM/OS上,我们可以从外边停止它。具体参见:
http://www.distributopia.com/servlet_stuff/background_threads.txt

解决方案 »

  1.   

    Control a thread from outside
    public class TT extends Thread {
     static final int RUN     = 0;
     static final int SUSPEND = 1;
     static final int STOP    = 2;
     private int state = RUN; public synchronized void setState(int s) {
      state = s;
      if (s == RUN) notify();
      } private boolean boolean checkState() {
      while (state ==  SUSPEND) {
       try {
        wait();
        }
       catch (Exception e) {}
       }
      if (state == STOP) 
       return false;
      return true;
      } public void run() {
      while true {
       doSomething();
       if (!checkState()) 
        break;
        }
     }