1.1.2 Thread State

Define six states: java.lang.Thread.State

  1. New: thread state of the thread has not been started.

  2. Runnable: Thread state runnable threads waiting for CPU scheduling.

  3. Blocked: the thread blocked waiting for the lock monitor thread state.

  4. Waiting: waiting thread state of the thread. Without timeout following way:

    Object.waiting、Thread.join、LockSupport.port

  5. Timed Waiting: waiting thread with a specified waiting time thread state. In the following manner with timeout:

    Thread.sleep、Object.wait、Thread.join、LockSupport.parkNanos、LockSupport.parkUntil

  6. Terminated: thread state to terminate the thread. Normal thread execution is completed or abnormal.

Screenshot 12.16.01.png 2020-02-12 PM

package firthChapter;

/**
 * 示例二,多线程运行状态切换示意
 * @author yekaifei
 */
public class Demo2 {
    //第一种状态切换: 新建->运行->终止


    //第二种状态切换: 新建->运行->等待->运行->终止
    //第三种状态切换: 新建->运行->阻塞->运行->终止
    public static void main(String[] args) throws InterruptedException {
        System.out.println("第一种状态切换: 新建->运行->终止");
        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                System.out.println("thread1当前状态:" + Thread.currentThread().getState().toString());
                System.out.println("thread1执行了");
            }
        });
        System.out.println("thread1未运行时状态:" + thread1.getState().toString());
        thread1.start();
        Thread.sleep(2000);
        System.out.println("线程睡眠两秒等待thread1运行完毕查看状态:" + thread1.getState().toString());
//        thread1.start();  TODO 注意若线程终止后再调用会报一个java.lang.IllegalThreadStateException

        System.out.println("-------------------------------------------------");
        System.out.println("第二种状态切换: 新建->运行->等待->运行->终止");
        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                System.out.println("thread2当前状态:" + Thread.currentThread().getState().toString());
                System.out.println("thread2正在执行");
                try {
                    System.out.println("thread线程进入等待");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("thread2睡眠完毕后当前状态:" + Thread.currentThread().getState().toString());
            }
        });
        System.out.println("thread2未运行时状态:" + thread2.getState().toString());
        thread2.start();
        Thread.sleep(1000);
        System.out.println("线程睡眠1s等待thread2进入睡眠查看状态:" + thread2.getState().toString());
        Thread.sleep(1000);
        System.out.println("线程再睡眠1s等待thread2运行完毕后查看状态:" + thread2.getState().toString());

        System.out.println("-------------------------------------------------");
        System.out.println("第三种状态切换: 新建->运行->阻塞->运行->终止");
        Thread thread3 = new Thread(new Runnable() {
            public void run() {
                System.out.println("thread3当前状态:" + Thread.currentThread().getState().toString());
                synchronized (Demo2.class){
                    System.out.println("thread3当前状态:" + Thread.currentThread().getState().toString());
                }
            }
        });
        synchronized (Demo2.class) {
            System.out.println("thread3未运行时状态:" + thread3.getState().toString());
            thread3.start();
            Thread.sleep(500);
            System.out.println("thread3进入阻塞状态:" + thread3.getState().toString());
        }
        Thread.sleep(2000);
        System.out.println("线程睡眠2s等待thread3运行完毕查看状态:" + thread3.getState().toString());
    }
}

运行结果:
firthChapter.Demo2
第一种状态切换: 新建->运行->终止
thread1未运行时状态:NEW
thread1当前状态:RUNNABLE
thread1执行了
线程睡眠两秒等待thread1运行完毕查看状态:TERMINATED
第二种状态切换: 新建->运行->等待->运行->终止
thread2未运行时状态:NEW
thread2当前状态:RUNNABLE
thread2正在执行
thread线程进入等待
线程睡眠1s等待thread2进入睡眠查看状态:TIMED_WAITING
thread2睡眠完毕后当前状态:RUNNABLE
线程再睡眠1s等待thread2运行完毕后查看状态:TERMINATED
第三种状态切换: 新建->运行->阻塞->运行->终止
thread3未运行时状态:NEW
thread3当前状态:RUNNABLE
thread3进入阻塞状态:BLOCKED
thread3当前状态:RUNNABLE
线程睡眠2s等待thread3运行完毕查看状态:TERMINATED
Process finished with exit code 0

Published 17 original articles · won praise 13 · views 911

Guess you like

Origin blog.csdn.net/weixin_43871142/article/details/104323262