标签:
线程对象在不同的运行时期有不同的状态,状态信息就存在于Thread内部类的State枚举类中
public enum State {
/**
* new状态是线程实例化后还从未执行start()方法时的状态
*/
NEW,
/**
* runnable状态是线程进人运行的状态
*/
RUNNABLE,
/**
* blocked状态出现在某一个线程在等待锁的时候。
*/
BLOCKED,
/**
* waiting是线程执行了Object.wait()方法后所处的状态
*/
WAITING,
/**
* timed_waiting代表线程执行了Thread.sleep()方法,
* 呈等待状态,等待时间到达,继续向下运行。
*/
TIMED_WAITING,
/**
* terinated是线程被销毁时的状态,线程完全执行了
*/
TERMINATED;
}
//该方法能获取线程的状态
public State getState() {
// get current thread state
return sun.misc.VM.toThreadState(threadStatus);
}
下面使用代码的方式验证线程所有的状态值,了解线程的状态有助于程序员监控线程对象所处的情况,比如哪些线程从未启动,哪些线程正在执行,哪些线程正在阻塞,哪些线程正在等待,哪些线程已经销毁了,等等。这些是与线程生命周期相关的信息。
首先验证的是new, runnable及terminated状态,new状态是线程实例化后还从未执行start()方法时的状态,而runnable状态是线程进入运行的状态,terinated是线程被销毁时的状态。
public class MyThread extends Thread {
public MyThread() {
System.out.println("构造方法中的状态:" + Thread.currentThread().getState());
}
@Override
public void run() {
System.out.println("run方法中的状态:" + Thread.currentThread().getState());
}
}public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread thread = new MyThread();
System.out.println("main方法中的状态1:" +thread.getState());
Thread.sleep(1000);
thread.start();
Thread.sleep(1000);
System.out.println("main方法中的状态2:" + thread.getState());
}
}
| 构造方法中的状态:RUNNABLE main方法中的状态1:NEW run方法中的状态:RUNNABLE main方法中的状态2:TERMINATED |
TIMED_WAITING代表线程执行了Thread.sleep()方法,呈等待状态,等待时间到达,继续向下运行。
public class MyThread extends Thread {
@Override
public void run() {
try {
System.out.println("begin sleep");
Thread.sleep(4000);
System.out.println(" end sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread thread = new MyThread();
thread.start();
//要验证timed_waiting,这里的睡眠时间要小于run方法里睡眠时间
Thread.sleep(1000);
System.out.println("main方法中的状态:" + thread.getState());
}
}
| begin sleep main方法中的状态:TIMED_WAITING end sleep |
| begin sleep end sleep main方法中的状态:TERMINATED |
BLOCKED状态出现在某一个线程在等待锁的时候。
public class MyService {
public synchronized static void serviceMethod() {
try {
System.out.println(Thread.currentThread().getName()
+ " 进入了业务方法!" );
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}public class MyThread1 extends Thread {
@Override
public void run() {
MyService.serviceMethod();
}
}public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread1 t1 = new MyThread1();
t1.setName("AA");
t1.start();
System.out.println("main方法中的t1状态:" + t1.getState());
MyThread1 t2 = new MyThread1();
t2.setName("BB");
t2.start();
Thread.sleep(1000);
System.out.println("main方法中的t2状态:" + t2.getState());
}
}
| AA 进入了业务方法! main方法中的t1状态:RUNNABLE main方法中的t2状态:BLOCKED BB 进入了业务方法! |
状态WAITING是线程执行了Object.wait()方法后所处的状态。
public class MyLock {
public static final Byte lock = new Byte("0");
}public class MyThread extends Thread {
@Override
public void run() {
try {
synchronized (MyLock.lock) {
MyLock.lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}public class Run {
public static void main(String[] args) throws InterruptedException {
MyThread t = new MyThread();
t.start();
Thread.sleep(1000); //如果没有,t的状态就是RUNNABLE
System.out.println("main方法中的t状态:" + t.getState());
}
}
| main方法中的t状态:WAITING |
Java多线程编程7--拾遗增补--线程的状态(new,runnable,terminated,timed_waiting,waiting,blocked)
标签:
原文地址:http://blog.csdn.net/ochangwen/article/details/51352946