我用一个map,在map中我加了一个id-------HashMap<String ,Thread> map = new HashMap<String ,Thread>();
然后我获得一个map,Thread th = map.get(ID);th.stop;为什么关闭不了线程呢

解决方案 »

  1.   

    先interrupt线程,再设置标志为假,退出run里面的循环
      

  2.   

    stop方法不安全,已经过时了,不推荐使用
      

  3.   

    stop已过时,并不能用此方法来停止一个线程
    假设线程当前状态是等待,包括使用sleep,wait等,可以使用interrupt来打断等待
    一般情况下,线程内部应该是出于一个while循环,所以一般来说while总是以一个boolean或者int标识作为循环标志的,类似这样的代码:public class TestThread extends Thread {
     private boolean running = true; public void run() {
      while(running) {
        // TODO 
      }
     } public void stopThread() {
      this.running = false;
     }
    }可以调用TestThread上的stopThead方法,使得线程在执行完当前一次操作之后跳出while循环来结束线程
      

  4.   

    中断:http://www.ticmy.com/?p=31
      

  5.   

    stop 已经过时了。调用该方法原理是抛出异常退出。
    把run方法里面的内容全部写在try 里面。
    try{
      }finally{
      return;
      

  6.   

    用try{
                     th.yield();
                 }
                 catch(Exception ex){
                     ex.printStackTrace();
                 }
    试试