文章目录
- 1. 线程的状态
- 2. 验证NEW、RUNNALE和TERMINATED状态
- 3. 验证TIMED_WAITING状态
- 4. 验证BLOCKED状态
- 5. 验证BLOCKED状态
1. 线程的状态
线程在不同的运行时期存在不同的状态,状态信息存在于State枚举类中,如下图:
调用线程有关的方法是造成线程状态改变的主要原因。State枚举类的源码如下:
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED;
}
- NEW:至今尚未启动的线程处于这种状态
- RUNNABLE:正在Java虚拟机中执行的线程处于这种状态
- BLOCKED:受阻塞并等待某个监视器所的线程处于这种状态
- WATING:无限期地等待另一个线程来执行某一特定操作的线程处于这种状态
- TIMED_WAITING:等待另一个线程来执行取决于指定等待时间的操作的线程处于这种状态
- TERMINATED:已退出的线程处于这种状态
2. 验证NEW、RUNNALE和TERMINATED状态
NEW状态是线程实例化后还未执行start()方法,RUNNALE状态是线程进入运行状态,TERMINATED是线程已被销毁。
public class Main {
public static void main(String[] args) {
try{
MyThread myThread=new MyThread();
System.out.println("main方法中的状态1(这里获得的是myThread的状态:"+myThread.getState());
Thread.sleep(1000);
myThread.start();
Thread.sleep(1000);
System.out.println("获得myThread线程的状态(这里获得的是Mythread销毁后的状态):"+myThread.getState());
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class MyThread extends Thread{
public MyThread(){
System.out.println("获得当前线程的状态(调用该类构造方法的线程仍然是主线程:"+Thread.currentThread().getState());
System.out.println("通过this获得创建的子线程的状态(也就是当前线程):"+this.getState());
}
@Override
public void run(){
System.out.println("run方法中的状态,也就是当前线程启动后的状态:"+Thread.currentThread().getState());
}
}
3. 验证TIMED_WAITING状态
状态TIMED_WAITING代表线程执行了Thread.sleep()方法,呈等地状态,等待时间达到后继续向下运行:
public class Main {
public static void main(String[] args) {
try{
MyThread myThread=new MyThread();
myThread.start();
Thread.sleep(1000);
System.out.println("获得myThread线程的状态(这里获得的是Mythread销毁后的状态):"+myThread.getState());
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class MyThread extends Thread{
@Override
public void run(){
System.out.println("begin sleep");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(" end sleep");
}
}
执行sleep()后线程的状态是TIMED_WAITING
4. 验证BLOCKED状态
BLOCKED状态出现在某一个线程在等待锁的时候
public class Main {
public static void main(String[] args) throws InterruptedException {
MyThread1 t1=new MyThread1();
t1.setName("a");
t1.start();
Thread.sleep(1000);
MyThread2 t2=new MyThread2();
t2.setName("b");
t2.start();
Thread.sleep(1000);
System.out.println("Main方法中的t2状态:"+t2.getState());
}
}
class MyThread1 extends Thread{
@Override
public void run(){
Myservice.ServiceMethod();
}
}
class MyThread2 extends Thread{
@Override
public void run(){
Myservice.ServiceMethod();
}
}
class Myservice{
synchronized static public void ServiceMethod(){
try {
System.out.println(Thread.currentThread().getName()+"进入业务方法!");
Thread.sleep(10000);
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
5. 验证BLOCKED状态
WAITING 是线程执行Object.wait()方法后所处在的状态
public class Main {
public static void main(String[] args) throws InterruptedException {
try {
MyThread t=new MyThread();
t.start();
Thread.sleep(1000);
System.out.println("main方法中t状态:"+t.getState());
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
class MyThread extends Thread{
@Override
public void run(){
try{
synchronized (Lock.lock){
Lock.lock.wait();
}
}catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
class Lock{
public static final Byte lock=new Byte("0");
}