创建线程的六种状态代码示例

  1. NEW : 线程刚刚创建,还没有启动

  2. RUNNABLE : 可运行状态,由线程调度器可以安排执行

    • 包括READY和RUNNING两种细分状态

  3. WAITING: 等待被唤醒

  4. TIMED WAITING: 隔一段时间后自动唤醒

  5. BLOCKED: 被阻塞,正在等待锁

  6. TERMINATED: 线程结束

package com.zy.c_000_threadbasic;

import com.zy.util.SleepHelper;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class To4_ThreadState {

    public static void main(String[] args) throws Exception {
        //========================================
        Thread t1 = new Thread(() -> {
            // 打印线程当前状态
            System.out.println("2: " + Thread.currentThread().getState()); // 2: RUNNABLE
            for (int i = 0; i < 3; i++) {
                // 休眠1秒
                SleepHelper.sleepSeconds(1);
                System.out.print(i + "  ");
            }
            System.out.println();
        });
        System.out.println("1: " + t1.getState()); // 1: NEW
        t1.start();
        t1.join();
        System.out.println("3: " + t1.getState()); // 3: TERMINATED


        //========================================
        Thread t2 = new Thread(() -> {
            // 调用该方法会阻塞住该线程。
            LockSupport.park();
            System.out.println("t2 go on!");
            SleepHelper.sleepSeconds(5);
        });
        t2.start();
        SleepHelper.sleepSeconds(1);
        System.out.println("4: " + t2.getState());//4: WAITING

        // 利用LockSupport.unpark()唤醒线程,该方法需要与LockSupport.park()方法成对出现,每次park都有对应的unpark。
        LockSupport.unpark(t2);
        SleepHelper.sleepSeconds(1);
        System.out.println("5: " + t2.getState());//5: TIMED_WAITING


        //========================================
        final Object o = new Object();
        Thread t3 = new Thread(() -> {
            synchronized (o) {
                System.out.println("t3 得到了锁 o");
            }
        });

        // 先启动一个线程,把o锁锁死
        new Thread(() -> {
            synchronized (o) {
                SleepHelper.sleepSeconds(5);
            }
        }).start();

        SleepHelper.sleepSeconds(1);
        t3.start();
        SleepHelper.sleepSeconds(1);
        System.out.println("6: " + t3.getState());// 6: BLOCKED


        //========================================
        // ReentrantLock是Lock的实现类,是一个互斥的同步锁。
        // 只有synchronize这样的锁才能进入block状态,其余锁进入不了
        final Lock lock = new ReentrantLock();
        Thread t4 = new Thread(() -> {
            lock.lock();
            System.out.println("t4 得到了锁 o");
            lock.unlock();
        });
        new Thread(() -> {
            lock.lock();
            SleepHelper.sleepSeconds(5);
            lock.unlock();
        }).start();

        SleepHelper.sleepSeconds(1);

        t4.start();
        SleepHelper.sleepSeconds(1);
        System.out.println("7: " + t4.getState());// 7: WAITING


        //========================================
        Thread t5 = new Thread(() -> {
            LockSupport.park();
        });
        t5.start();

        SleepHelper.sleepSeconds(1);

        System.out.println("8: " + t5.getState());// 8: WAITING
        LockSupport.unpark(t5);


    }
}

package com.zy.util;

import java.util.concurrent.TimeUnit;

public class SleepHelper {

    // 线程休眠
    public static void sleepSeconds(int seconds){
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(java,java,jvm,开发语言)