在线程中的的isAlive()方法基本上是一个无用的方法,说他无用主要是它的线程以前的状态,不是现在的状态。建议你用用阻挡层,这样你的线程就要强壮多了!

解决方案 »

  1.   

    对,用Interrupt().以下是对比:
    isAlive
    public final boolean isAlive()
    Tests if this thread is alive. A thread is alive if it has been started and has not yet died. Returns:
    true if this thread is alive; false otherwise.
    interrupt
    public void interrupt()
    Interrupts this thread. 
    First the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown. If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException. If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a ClosedByInterruptException. If this thread is blocked in a Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked. If none of the previous conditions hold then this thread's interrupt status will be set. 
    Throws: 
    SecurityException - if the current thread cannot modify this thread
      

  2.   

    简略贴一下代码(因为动画那里太长)
    applet  里面的
    run()函数
    public void run()
    {
     A a=new A(this);   //因为线程里面要用到applet的paint();
     B b=new B(this,a); //同上,
     a.start();
     b.start();
    }
    线程A的run()函数:
    public void run()
    {
     for(;;)

      ……  //循环动画内容
      ……
     }
    }
    线程B的run()函数:
    public void run()
    {
     ……
     ……
     ……    //动画内容
     if(a.isAlive())
      a.stop();
     ……
     ……
     ……
    }
      我在debug的时候在B的run()函数开头设了中断,
      对a.isAlive()加了watch,单步运行开始结果为true
      不过运行了几步(判断之前)结果就变为false了
       但是我看那个动画运行,由a线程负责的动画效果
      还在继续。就是这样了,请帮忙一下