在Java API Docs中已经不赞成使用stop()来强行使线程进入死亡,因为它不释放线程所取得的机锁(lock),destroy()拥有死锁问题。
如果你的线程中使用了一个死循环来进行线程中的处理:
while(true){
  .................;
}
那么建议把 true 改为一个boolean的变量,然后写一个方法将这个boolean变量改为false(在按钮的事件中调用这个方法),这样就可以使线程自然的进入死亡。
<------ 树欲静而风不止 ------>

解决方案 »

  1.   

    同上的方法,描述一下public void run(){
        boolean flag=true;
        while(flag){
            ................
        }
    }按钮的doClick的处理方法中,让flag=false;
      setFlag(flase);上面的线程自然死亡。
      

  2.   

    方法1:
    volatile boolean isAlivepublic void run()
    {
         while(isAlive)
         {
         }
    }方法2:
    boolean isAlive;
    public void setAlive(boolean flag)
    {
        synchronized(this)
        {
            isAlive=flag;
        }
    }
    public void run()
    {
         while(isAlive)
         {
         }
    }
      

  3.   

    volatile boolean wantExit = false;
    Thread t = new Thread(){
      public void run()
      {
        while(! wantExit){
            //add your code here
        }
      }
    };
    public void button1_actionPerformed()
    {
       wantExit = true;
       t.join(); //wait for thread to exit
       System.out.println("thread stoped!");
    }
      

  4.   

    你可以换一种方法!while(th&&Integer.parseInt(fen.getText())>0)
      {
     try
         {
    Thread.sleep(1000);
        int t=Integer.parseInt(fen.getText());
      t-=1;
      fen.setText(String.valueOf(t));
     }
     catch(Exception e)
     {}