在JDK1.5中有getState()方法和Thread.State类可以判断线程的状态,但是现在老大说必须在1.4上实现多线程的一些功能,怎么才能在1.4上判断线程是否正常结束,是否是新线程……,???
[b]问题补充:[/b]
不好意思,可能是我的问题没有描述清楚.我再重新说明一下.现在要求在JDK1.4版本上进行一个多线程的开发.在这么几种状态需要判断:1.线程是否是新建未执行的;2.线程是否是在执行中;3.线程是否正常执行完毕;4.线程是否发生异常。而isInterrupted()只是测试是否异常中止,isAlive()也只是测试线程是否还活着,但相关文档并没有说明如果isAlive()返回false则表明线程正常中止。
于是我想到继承Thread类,然后添加一个线程状态,在构造方法中将状态置为新增,在start()方法前将线程置为已启动:
[code="java"]
public class ThreadTask extends Thread {
/**
* 线程状态:新建
*/
public final static int THREAD_STATE_NEW = 0;
/**
* 线程状态:执行中
*/
public final static int THREAD_STATE_EXEC = 1;
/**
* 线程状态:意外中止
*/
public final static int THREAD_STATE_INTERRUPTED = 2;
/**
* 线程状态:执行完毕
*/
public final static int THREAD_STATE_OVER = 3;
/**
* 线程状态
*/
private int state = 0;
public ThreadTask(ThreadGroup tg, Runnable run) {
super(tg, run);
chgStat(THREAD_STATE_NEW);
}
public void start() {
chgStat(THREAD_STATE_EXEC);
super.start();
}
private synchronized void chgStat(int stat) {
this.state = stat;
}
}
[/code]
但是应该如何确定执行正常执行完毕呢?
你的思路,给我说的一样,你自己继承1.4中的Thread,就是说扩展它的功能,上面已经说了,你可以参考JDK1.5中的设计,但是难点就是:里面这个方法
[code="java"]public State getState() {
// get current thread state
return sun.misc.VM.toThreadState(threadStatus);
} [/code]
会调用sun.misc.VM.toThreadState(int threadStatus)这个方法,关于这个类,我也把网址给你了,你自己看看吧
public boolean isInterrupted()
测试线程是否已经中断。线程的中断状态 不受该方法的影响
public final boolean isAlive()
测试线程是否处于活动状态。如果线程已经启动且尚未终止,则为活动状态。
public final boolean isAlive()
这个方法只能判断线程是否处在活动状态,如果想准确判断,还有点难度。
如果楼主想准确判断线程处于什么状态,个人建议研究SE5中的Thread,尤其是和状态相关的内容,例如:枚举State和getState()方法。
然后你自己写一个类继承1.4中的Thread。向其中添加相应的方法。
以下代码为1.5中的代码:
[code="java"] public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
/**
* Returns the state of this thread.
* This method is designed for use in monitoring of the system state,
* not for synchronization control.
*
* @return this thread's state.
* @since 1.5
*/
public State getState() {
// get current thread state
return sun.misc.VM.toThreadState(threadStatus);
}[/code]
在该段代码中,其实最重要的方法就是:
[code="java"]sun.misc.VM.toThreadState(int threadStatus)[/code]方法。
sun的官方文档有这个类的说明,你可以看看
[url]http://www.docjar.com/html/api/sun/misc/VM.java.html[/url]