当前位置:网站首页>Thread interview related questions

Thread interview related questions

2022-06-10 17:01:00 Zhangzhiming (hard work version)

What are the states of a thread ?
       Java There are 6 States , Respectively :NEW、RUNNABLE、WAITING、TIMED_WAITING、BLOCKED、TERMINATED, among RUNNABLE contain READY and RUNNING state , The details are shown in the figure below :


  • 【NEW】: The initial state , Threads are built , But not yet called start Method
  • 【RUNNABLE】: stay Java There are two states in ,RUNNING and READY
           READY: The ready state , The thread in this state has obtained all the resources needed for execution , as long as CPU Assign executive power to run ; All ready threads are stored in the ready queue
          RUNNING: get cpu Time slice , Executing thread ; Because each cpu Only one thread can be executed at the same time , So every cpu Only one thread runs at a time .
     
  • 【BLOCKED】: Blocked state , Indicates that the thread is waiting , In other words, the thread gave up for some reason CPU Right to use , Congestion can also be divided into several situations :
    • Waiting for a jam : The running thread executes Thread.sleep 、wait()、 join() Other methods JVM The current thread will be set to the waiting state , When sleep end 、join After the thread terminates or the thread is awakened , The thread goes from a waiting state to a blocking state , Re preempt the lock and resume the thread
    • Synchronous blocking : When a running thread acquires a synchronized lock for an object , If the synchronization lock is occupied by another thread lock , that jvm Will put the current thread into the lock pool
    • Other blockages : Issued I/O When asked ,JVM Will set the current thread to a blocked state , When I/O After processing, the thread will resume
  • 【WAITING】: A thread entering this state needs to wait for some specific action from another thread ( Notification or interruption )
    • perform wait()、join()、LockSupport.park()
  • 【TIMED_WAITING】: Timeout wait status , This state is different from WAITING, It can return on its own after a specified time
    • perform Thread.sleep(long)、wait(long)、join(long)、LockSupport.park(long)、LockSupport.parkNanos(long)、LockSupport.parkUntil(long)
  • 【TERMINATED】: Termination status , Indicates that the current thread has finished executing

  Thread calls start Is the method executed immediately ?
         Threads do not execute immediately ; To be precise , call start( ) After the method , The state of the thread changes from new The state of become “READY( be ready )” state , instead of “RUNNING( Running )” state . The thread has to wait CPU Dispatch , Different JVM There are different scheduling algorithms , When a thread is scheduled is unknown . therefore ,start() The order in which methods are called does not determine the order in which threads are executed

Java Threads 6 States VS 5 States
         At the operating system level 5 States ( newly build 、 be ready 、 function 、 Blocking 、 End )
① be assigned to CPU The time of the : function
② Can be assigned to the CPU The time of the : be ready
③ I can't get it CPU The time of the : Blocking
        Java Medium RUNNABLE Covers readiness 、 function 、 Blocking I/O

  • 【 New state 】: Only thread objects are created at the language level , Not yet associated with an operating system thread
  • 【 Operational state ( Ready state )】: Indicates that the thread has been created ( Associated with the operating system thread ), Can be CPU Scheduling execution
  • 【 Running state 】: Refers to the acquisition of CPU Time slice running state
    • When CPU I ran out of time , From 【 Running state 】 Switch to 【 Operational state 】, Causes a thread context switch
  • 【 Blocked state 】
    • If blocking is called API, Such as BIO Read and write files , At this time, the thread will not actually use CPU, Causes a thread context switch , Get into 【 Blocked state 】
    • etc. BIO Operation completed , Blocked threads will be awakened by the operating system , Switch to 【 Operational state 】
    • And 【 Operational state 】 Is the difference between the , Yes 【 Blocked state 】 As long as they don't wake up , The scheduler will never consider scheduling them
  • 【 Termination status 】: Indicates that the thread has finished executing , The life cycle is over , It will not switch to other states

原网站

版权声明
本文为[Zhangzhiming (hard work version)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101600053002.html

随机推荐