1.1.2 线程状态

2020-02-12  本文已影响0人  叶凯飞

6个状态的定义:java.lang.Thread.State

  1. New: 尚未启动的线程的线程状态。

  2. Runnable: 可运行线程的线程状态,等待CPU的调度。

  3. Blocked: 线程阻塞等待监视器锁定的线程状态。

  4. Waiting: 等待线程的线程状态。下列不带超时的方式:

    Object.waiting、Thread.join、LockSupport.port

  5. Timed Waiting: 具有指定等待时间的等待线程的线程状态。下列带超时时间的方式:

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

  6. Terminated: 终止线程的线程状态。线程正常完成执行或者出现异常。

截屏2020-02-12下午12.16.01.png
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

上一篇下一篇

猜你喜欢

热点阅读