首页 > 其他分享 >线程的状态以及各个常常见方法(sleep,join,yied,interrupt)复习

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

时间:2023-02-23 19:11:07浏览次数:30  
标签:join Thread thread yied state waiting 线程 wait

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(清空还原为默认值)

标签:join,Thread,thread,yied,state,waiting,线程,wait
From: https://www.cnblogs.com/gaokunlong/p/17149110.html

相关文章

  • 进程与线程
    进程Hello.java编译并运行Hello.javajps查询java当前运行的进程24900Hello表示名为Hello的进程正在运行关闭运行Hello.java的cmd后再查询jpsHello进程消失了......
  • 【人脸检测】(MTCNN) Joint Face Detection and Alignment using Multi-task Cascaded
    原始题目JointFaceDetectionandAlignmentusingMulti-taskCascadedConvolutionalNetworks中文名称基于多任务级联卷积网络的联合人脸检测与对齐......
  • java中四种创建线程的方式
    第一种方式:通过编写类继承Thread,重写run方法实现 实现实例:publicclassThreadTest{publicstaticvoidmain(String[]args){System.out.println("ma......
  • Android----->多线程的实现Thread、IntentService的运用
       首先建立一个Intent.xml <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"......
  • 多线程
    一、多线程介绍1、Java中创建线程的方法3种:Thread、Runnable、Callable继承Thread类(Thread类本身就是实现Runnable接口的)实现Runnable接口实现Callable接口2、继承......
  • 【HashMap】HashMap多线程下的死循环问题及JDK8版本的修复
    背景想要记录一下关于jdk下的hashmap存在的一些问题:1、许多同学都知道JDK下的HashMap是线程不安全的,但究竟是怎样个不安全法,在多线程下会出现怎样的问题?其中原因是......
  • TFlite 多线程并行
    importnumpyasnpfrommultiprocessingimportProcess,Queuedefprocess_data(data,model_TSNet,tsnet_input,tsnet_output,queue):model=tflite_runti......
  • c++线程的使用
    c++11之后,c++语言提供了并发编程的语言支持。c++11增加了线程以及线程相关的类。c++11提供的线程类叫做std::thread,创建线程只需提供线程函数或者函数对象,并且可以指定参......
  • java 线程的同步
     https://www.cnblogs.com/yihujiu Example12_7.javapublicclassExample12_7{publicstaticvoidmain(Stringargs[]){Bankbank=newBank();......
  • java 协调同步的线程
    Example12_8.javapublicclassExample12_8{publicstaticvoidmain(Stringargs[]){TicketHouseofficer=newTicketHouse();Threadzhangfe......