现在我的主程序启动了一个子线程, 请问如何判断子线程是否结束, 我目前用得是r.getState()==Thread.State.TERMINATED//其中r指的是该子线程,这种方法可以吗,有没有更好的方法? 谢谢

解决方案 »

  1.   

    可以自己定义一个标志啊,在线程的run方法里面自己修改这个标志。
      

  2.   

    In JAVA:
    You can see some follow methods for this current Thread:boolean isAlive = Thread.currentThread().isAlive();
    boolean isDead = Thread.currentThread().isDead();
    boolean isInterupt = Thread.currentThread().isInterrupted();
    /**
     * Answers <code>true</code> if the receiver has
     * already been started and still runs code (hasn't died yet).
     * Answers <code>false</code> either if the receiver hasn't been
     * started yet or if it has already started and run to completion and died.
     *
     * @version initial
     *
     * @return a <code>boolean</code>
     *
     * @see Thread#start
     */
    public final boolean isAlive() {
    synchronized(lock) {
    return threadRef != NO_REF;
    }
    }/**
     * Answers <code>true</code> if the receiver has
     * already died and been removed from the ThreadGroup
     * where it belonged.
     *
     * @version initial
     *
     * @return a <code>boolean</code>
     *
     * @see Thread#start
     * @see Thread#isAlive
     */
    private boolean isDead() {
    // Has already started, is not alive anymore, and has been removed from the ThreadGroup
    synchronized(lock) {
    return started && threadRef == NO_REF;
    }
    }
    /**
     * Answers a <code>boolean</code> indicating whether the receiver
     * has a pending interrupt request (<code>true</code>) or not (<code>false</code>)
     *
     * @version initial
     *
     * @return a <code>boolean</code>
     *
     * @see Thread#interrupt
     * @see Thread#interrupted
     */
    public boolean isInterrupted() {
    synchronized(lock) {
    return isInterruptedImpl();
    }
    }Please contact with me if have any question for this problem.
      

  3.   

    自己定义一个boolean类型的标志boolean done = false;
    while (!done) {
    //.....
    }
    当done==true时线程就终止了。
      

  4.   


    我觉得你这样就可以了阿
    或者使用Thread类的isAlive() 方法也是一个道理了
      

  5.   

    isAlive()直接判断就可以了,当然,你的方法也可以