线程的状态以及各个常常见方法(sleep,join,yied,interrupt)复习

1.线程的状态以及常见状态复习

线程的几个状态为,new(初始状态),runable(可以执行状态),running(运行状态),blocked(阻塞状态),dead(结束状态)。
常见的状态之间的转换如下:

但是在java的Thread类里的状态枚举,并没有一一对应上述的5个状态,而是如下

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;
    }

少了结束状态,而把阻塞状态分成了3个更为详细的分类,WAITING(调用wait(),LockSuport.park()方法阻塞),TIMED_WAITING(调用带有时间的wait或者sleep方法阻塞),BLOCKED(竞争Sychronized锁阻塞)。
而状态之间的转换图常见的如下:

但是今天看到的一张图比较符合我脑袋中学习到的知识,如下:

把notify唤醒后的工作描述出来,即把唤醒的线程会放到一个等待的队列里,也让我回忆起了notify会出现的问题,即可能没有唤醒想要唤醒的线程,所以要用notifyAll替换notyify

2.线程的主要常用方法

线程的常见方法如下:

上面图片中的interrupt方法的备注我有所要说,这个“打断”用词只是这个方法名的名称。分三种情况:
1.在一个因为用了sleep,wait,join方法而阻塞的线程,其他线程调用这个线程的打断方法,回在调用前面三种方法的地方抛出InterruptException,调用sleep,wait,join方法必须要catch这个异常,否则编译报错,那么被中断后抛出这个异常,即打断了阻塞状态,将其唤醒,只不过代码会走到catch 异常的代码块,那么接下来怎么处理看实际需要在catch中实现。
2.对一个正常运行的线程调用interrupt方法,只会讲其中的中断标志设置成true,其他什么不干。
3.对于一个因为调用了LockSuport.park方法阻塞的线程调用interrupt方法,这个线程会被唤醒并继续执行park后面的代码,不过这个线程的中断标志已经被设置true;这种情况下后面在怎么执行park方法也无法讲线程阻塞。

另外还有两个方法,isInterrupted,interrupted方法,这两个方法和interrupt方法很类似。单功能完全不同
isInterrupted:就是个get方法,返回线程的中断状态;
interrupted:返回中断状态的同时,将中断状态设为false(清空还原为默认值)

posted @ 2023-02-23 19:05  龙祭司  阅读(45)  评论(0)    收藏  举报