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.
自己定义一个boolean类型的标志boolean done = false; while (!done) { //..... } 当done==true时线程就终止了。
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.
while (!done) {
//.....
}
当done==true时线程就终止了。
我觉得你这样就可以了阿
或者使用Thread类的isAlive() 方法也是一个道理了