如何判断一个线程是否在运行中?
我现在有很多个线程在一起跑,但我不知道哪个线程已经推出了run函数,怎么判断?

解决方案 »

  1.   

    一般写线程的时候,我都会定义一个boolean stopped变量,在run的while循环里使用这个变量来判断是否继续执行循环中的语句:public void run() {
        while (!stopped) {
              // code........
        }
        stopped = true;
    }
      

  2.   

    public final boolean isAlive()    Tests if this thread is alive. A thread is alive if it has been started and has not yet died. 
      

  3.   

    用 isAlive()方法     
    if(thread1.isAlive()){
      System.out.println("thread1 is alive");
    }
    else
    {
    System.out.println("thread2 is not alive");
    }