当前位置:网站首页>What states do threads have?

What states do threads have?

2022-06-12 06:09:00 Z boo boo

The state of the thread is in Java The level is slightly different from the level of the operating system , The following from Java And operating system

Java Threads are divided into six states

  • NEW New state

The newly created thread has not been associated with the underlying thread of the operating system , It can only be said that it was just new Out object

  • RUNNABLE Operational state

Runnable state means that the thread in the new state calls start After method , It enters the operational state , Only runnable threads are associated with the underlying threads of the operating system

  • TERMINATED Termination status

When the code executed by the thread is finished , Enter this end state

  • BLOCKED Blocked state

A blocking state is when multiple threads access a shared resource , Access to resources is obtained by obtaining locks , When the lock acquisition fails, it will enter the blocking state , Of course, threads in a blocked state can still compete for locks , When it acquires the lock, it changes from the blocking state to the operable state

  • WAITING Wait state

The waiting state is like this , When the thread acquires the lock successfully , However, it cannot be continued for some reasons , It will be called by wait Method then enters the waiting state , A thread in a waiting state , call notify Method to return from the waiting state to the runnable state

  • TIMED_WAITING Time limited waiting state

The principle of time limited waiting state is similar to that of waiting state , It just adds a waiting implementation , The original normal wait state is only called notify Method before returning to the runnable state , But the time limited waiting state , If you are not awakened after the time has expired, you will automatically wake up and return to the operable state
Entering a time limited wait state, you can call the with parameters wait(long) Method , And you can call sleep(long) Method

  • Java Level thread state transition diagram
     Please add a picture description

The operating system level is divided into five states

  • The operating system level will Java The operational state of the layer is split into two states , One is ready , One is running status
  • take Java Blocking in , wait for , Time limited waiting is called blocking state
  • New state

Threads have just been created , Has not been assigned to CPU The ability of time slice

  • Ready state

New thread calls start After the method , The state the thread enters , At this point, the thread has the ability to rob cpu The ability of time slice

  • Running state

When a thread is assigned to cpu Time slice time , It goes into operation , Execute the relevant code

  • Blocked state

The thread encounters some blocking time during execution

  • Termination status

The thread task will enter this termination state after execution

  • Operating system level thread state transition diagram
     Please add a picture description
原网站

版权声明
本文为[Z boo boo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120600550675.html