线程Tread的6种状态及转换关系

2020-10-22  本文已影响0人  陈萍儿Candy

1.Thread中的内部类State

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * 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.
         */
        RUNNABLE,

        /**
         * 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
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * 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:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

2.Thread状态转换图


image.png

3.上面1.2,已经把状态介绍完,以下是遇到问题记录:
部分测试代码:

@Override
    public void run() {
        super.run();
        Log.i("cyp", "run: thread1");
//        Log.i("cyp", "thread1:"+this.getState());
//        Log.i("cyp", "thread1:"+this.getThreadGroup().getName());
//        Log.i("cyp", "thread1: "+this.getThreadGroup().activeCount());
        synchronized (lockObject) {
            try {
                Log.i("cyp", "Thread1: synchronized");
                Log.i("cyp", "Thread1   wait 方法执行前");
                lockObject.wait(10000);
                Log.i("cyp", "Thread1   wait 方法执行后");
                sleep(10000);
                Log.i("cyp", "Thread1   sleep方法结束");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

记录:执行lockObject.wait(10000);后线程的状态由RUNNABLE转化为TIMED_WAITING,打印出“Thread1 wait 方法执行后”后也就是wait(10000)10秒后抢到锁,继续执行,此时线程的状态由TIMED_WAITING转化为RUNNABLE;如果执行了lockObject.wait(10000);等待10秒后开始抢锁,但是锁 lockObject没有空闲,此时线程进入阻塞BLOCKED状态,直到抢到锁进入RUNNABLE;

4.总结:
线程创建出来就是new的状态。

start方法调用之后状态变成runnable,可以执行,如果cpu把时间片切过来就可以执行。

runnable执行完成则变成terminated,或者中断的时候。

runnable如果遇到有锁的方法,在等待的时候则是阻塞blocked的状态

如果调用wait或者join的方法,则变成waiting状态,

如果调用带时间参数的wait或者join,则变成time_waiting。

上一篇下一篇

猜你喜欢

热点阅读