代码模拟线程的6种状态

2022-03-03  本文已影响0人  lconcise

线程6种状态:

Thread state for a thread which has not yet started.

Thread state for a runnable thread. A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.

Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:

  • Object.wait with no timeout
  • Thread.join with no timeout
  • LockSupport.park

A thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

  • Thread.sleep
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

Thread state for a terminated thread. The thread has completed execution.

代码模拟线程的6种状态

public class ThreadState {

    public static void main(String[] args) throws Exception {
        Thread t1 = new Thread(() ->{
            System.out.println("2:"+Thread.currentThread().getState());
            for (int i = 0;i < 3;i++){
                SleepHelp1.sleepseconds(1);
                System.out.print(i+" ");
            }
            System.out.println();
        });
        System.out.println("1:"+t1.getState());
        t1.start();
        t1.join();
        System.out.println("3:"+t1.getState());

        //=====================================================================
        Thread t2 = new Thread(()->{
            //阻塞  等着被叫醒  waiting
            LockSupport.park();
            System.out.println("t2 go on");
            SleepHelp1.sleepseconds(5);

        });
        t2.start();
        SleepHelp1.sleepseconds(1);
        System.out.println("4:"+t2.getState());

        //叫醒线程
        LockSupport.unpark(t2);
        SleepHelp1.sleepseconds(1);
        System.out.println("5:"+t2.getState());
        //=====================================================================

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

        new Thread(()->{
            synchronized (o){
                SleepHelp1.sleepseconds(5);
            }
        }).start();

        SleepHelp1.sleepseconds(1);

        t3.start();

        SleepHelp1.sleepseconds(1);

        System.out.println("6:"+t3.getState());
        //===========================================================
        //juc的锁 盲等待, 不会进入 block 的状态,进入 waiting 状态
        final Lock lock = new ReentrantLock();
        Thread t4 = new Thread(()->{
            lock.lock();//省略掉 try ... finally
            System.out.println("t4 拿到了锁 o");
            lock.unlock();
        });

        new Thread(()->{
            lock.lock();
            SleepHelp1.sleepseconds(5);
            lock.unlock();
        }).start();

        SleepHelp1.sleepseconds(1);

        t4.start();

        SleepHelp1.sleepseconds(1);

        System.out.println("7:"+t4.getState());
        //=======================================================
        Thread t5 = new Thread(()->{
            LockSupport.park();
        });

        t5.start();

        SleepHelp1.sleepseconds(1);

        System.out.println("8:"+t5.getState());

        SleepHelp1.sleepseconds(1);

        LockSupport.unpark(t5);
    }

}

class SleepHelp1 {

    public static void sleepseconds(int seconds){
        try {
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

执行结果:

1:NEW
2:RUNNABLE
0 1 2 
3:TERMINATED
4:WAITING
t2 go on
5:TIMED_WAITING
6:BLOCKED
7:WAITING
t3 得到了 锁 o
8:WAITING
t4 拿到了锁 o
上一篇下一篇

猜你喜欢

热点阅读