线程状态转换

@Test
public void testThreadState1() throws Exception {
    Thread thread4TestState = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("1" + "-" + Thread.currentThread().getName() + "的状态是:" + Thread.currentThread().getState());
        }
    },"thread4TestState");
    System.out.println("2" + "-" + thread4TestState.getName() + "的状态是:" + thread.getState());
    thread.start();
    System.out.println("3" + "-" + thread4TestState.getName() + "的状态是:" + thread.getState());
    Thread.sleep(2000);
    System.out.println("4" + "-" + thread4TestState.getName() + "的状态是:" + thread.getState());
    System.in.read();
}

上面的输出是:

  • new一个新的线程,没有调用start()

    输出:2-thread4TestState的状态是:NEW

  • 调用start()

    输出:3-thread4TestState的状态是:RUNNABLE

  • run()方法里;

    输出:1-thread4TestState的状态是:RUNNABLE

  • run()执行完了;

    输出:4-thread4TestState的状态是:TERMINATED

@Test
public void testThreadState2() throws Exception {
    Object lock = new Object();
    Thread thread4TestState1 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock){
                System.out.println("1" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("2" + "-" + Thread.currentThread().getName() + "释放了锁,当前的状态是:" + Thread.currentThread().getState());
        }
    }, "thread4TestState1");
    Thread thread4TestState2 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock){
                System.out.println("3" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("4" + "-" + Thread.currentThread().getName() + "释放了锁,当前的状态是:" + Thread.currentThread().getState());
        }
    }, "thread4TestState2");

    thread4TestState1.start();
    thread4TestState2.start();
    Thread.sleep(500);
    System.out.println("5" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
    System.out.println("6" + "-" + thread4TestState2.getName() + "当前的状态是:" + thread4TestState2.getState());
    System.in.read();

}

上面的输出是:

  • 首先thread4TestState1获取到锁,进入同步代码块;

    输出:1-thread4TestState1获取到了锁,当前的状态是:RUNNABLE

  • 然后,thread4TestState1睡1000ms,

    那在500ms时查看,由于调用了sleep()

    thread4TestState1的状态就是TIMED_WAITING

    输出:5-thread4TestState1当前的状态是:TIMED_WAITING

  • thread4TestState2由于thread4TestState1还没睡醒,所以还没释放锁,那thread4TestState2也就还在等待获取锁,所以状态就是BLOCKED

    输出:6-thread4TestState2当前的状态是:BLOCKED

  • 1000ms过去了,thread4TestState1睡醒了,释放了锁,状态还是RUNNABLE

    输出:2-thread4TestState1释放了锁,当前的状态是:RUNNABLE

  • 由于thread4TestState1释放了锁,那thread4TestState2就可以拿到锁,状态从BLOCKED变为RUNNABLE

    输出:3-thread4TestState2获取到了锁,当前的状态是:RUNNABLE
    输出:4-thread4TestState2释放了锁,当前的状态是:RUNNABLE

@Test
public void testThreadState3() throws Exception {
    Object lock = new Object();
    Thread thread4TestState1 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock){
                System.out.println("1" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
                try {
                    lock.wait(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2" + "-" + Thread.currentThread().getName() + "即将释放锁,当前的状态是:" + Thread.currentThread().getState());
            }
        }
    }, "thread4TestState1");

    thread4TestState1.start();
    Thread.sleep(300);
    System.out.println("3" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
    System.in.read();
}

上面的输出是:

  • 首先获取到了锁

    输出:1-thread4TestState1获取到了锁,当前的状态是:RUNNABLE

  • 获取到锁的线程调用了wait(timeout)

    输出:3-thread4TestState1当前的状态是:TIMED_WAITING

  • 线程从TIMED_WATING状态恢复

    输出:2-thread4TestState1即将释放锁,当前的状态是:RUNNABLE

@Test
public void testThreadState3() throws Exception {
    Object lock = new Object();
    Thread thread4TestState1 = new Thread(new Runnable() {
        @Override
        public void run() {
            synchronized (lock){
                System.out.println("1" + "-" + Thread.currentThread().getName() + "获取到了锁,当前的状态是:" + Thread.currentThread().getState());
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("2" + "-" + Thread.currentThread().getName() + "即将释放锁,当前的状态是:" + Thread.currentThread().getState());
            }
        }
    }, "thread4TestState1");

    thread4TestState1.start();
    Thread.sleep(50);
    System.out.println("3" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
    synchronized (lock){
        lock.notify();
    }
    System.out.println("4" + "-" + thread4TestState1.getName() + "当前的状态是:" + thread4TestState1.getState());
    System.in.read();
}

上面的输出是:

  • 首先获取到了锁

    输出:1-thread4TestState1获取到了锁,当前的状态是:RUNNABLE

  • 线程调用了无参的wait()

    输出:3-thread4TestState1当前的状态是:WAITING

  • 主线程执行了notify操作,唤醒处于WAITING的线程,输出有可能是RUNNABLE或者BLOCKED

    输出:4-thread4TestState1当前的状态是:RUNNABLE或者BLOCKED

  • 输出:2-thread4TestState1即将释放锁,当前的状态是:RUNNABLE

上一篇:自旋锁


下一篇:windows 远程桌面连接提示:出现了内部错误,windows远程桌面登录linux闪退