并发编程

一、线程的状态转换

2020-08-29  本文已影响0人  liyc712

线程的状态转换

threadStatus_01.png

线程的6个状态

New

线程刚创建的状态;

Runnable

调用start()后的状态,可以对应操作系统的ready running状态;

Blocked

进入到synchronized修饰的代码块或方法并已经被其他线程获得monitor锁;
注意:一定是遇到synchronized才会出现,其他的锁虽然也会是线程陷入等待,但是不会使线程从Runnable进入到Blocked状态;

当获得了monitor锁就会回到Runnable状态;

Waiting

使线程从Runnable进入到Waiting等待的方法

唤醒正在等待线程的方法,是线程从Waiting到Runnable

Timed Waiting

使线程从Runnable进入到Timed Waiting倒计时等待的方法

唤醒倒计时正在等待线程的方法,是线程从Timed Waiting到Runnable
等待时间到

Terminated

执行完成或者抛出异常

注意:

展示线程的NEW、RUNNABLE、Terminated状态。即使是正在运行,也是Runnable状态,而不是Running。

/**
 * 描述:展示线程的NEW、RUNNABLE、Terminated状态。即使是正在运行,也是Runnable状态,而不是Running。
 */
public class NewRunnableTerminated implements Runnable {

    public static void main(String[] args) {
        Thread thread = new Thread(new NewRunnableTerminated());
        //打印出NEW的状态
        System.out.println(thread.getState());
        thread.start();
        System.out.println(thread.getState());
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //打印出RUNNABLE的状态,即使是正在运行,也是RUNNABLE,而不是RUNNING
        System.out.println(thread.getState());
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //打印出TERMINATED状态
        System.out.println(thread.getState());
    }

    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
            System.out.println(i);
        }
    }
}
    /**
     *
     * 输出结果:
     * NEW
     * RUNNABLE
     * 0
     * 1
     * 2
     * 3
     * ……
     * RUNNABLE
     * ……
     * 998
     * 999
     * TERMINATED
     *
     *
     */

展示Blocked, Waiting, TimedWaiting

**
 * 描述:展示Blocked, Waiting, TimedWaiting
 */
public class BlockedWaitingTimedWaiting implements Runnable{
    public static void main(String[] args) {
        BlockedWaitingTimedWaiting runnable = new BlockedWaitingTimedWaiting();
        Thread thread1 = new Thread(runnable);
        thread1.start();
        Thread thread2 = new Thread(runnable);
        thread2.start();
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //打印出Timed_Waiting状态,因为正在执行Thread.sleep(1000);
        System.out.println(thread1.getState());
        //打印出BLOCKED状态,因为thread2想拿得到sync()的锁却拿不到
        System.out.println(thread2.getState());
        try {
            Thread.sleep(1300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //打印出WAITING状态,因为执行了wait()
        System.out.println(thread1.getState());

    }

    @Override
    public void run() {
        syn();
    }

    private synchronized void syn() {
        try {
            Thread.sleep(1000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 输出:
     * TIMED_WAITING
     * BLOCKED
     * WAITING
     */
}
上一篇下一篇

猜你喜欢

热点阅读