"Technology sharing" java thread state transition between each other, look at this

wavefar 2021-04-08 12:27:04
technology sharing java thread state

Java The status of threads in is divided into 6 Kind of .

1. initial (NEW): A new thread object is created , But it's not called yet start() Method .

2. function (RUNNABLE):Java The thread will be ready (ready) And in the operation of the (running) The two states are generally referred to as “ function ”. After the thread object is created , Other threads ( such as main Threads ) Called the object's start() Method . The thread in this state is in the runnable thread pool , Waiting to be selected by thread scheduling , obtain CPU Right to use , It's in a ready state (ready). The thread in the ready state is getting CPU After the time slice, it becomes the running state (running).

3. Blocking (BLOCKED): Indicates that the thread is blocked by a lock .

4. wait for (WAITING): A thread entering this state needs to wait for some specific action from another thread ( Notification or interruption ).

5. Overtime waiting (TIMED_WAITING): This state is different from WAITING, It can return on its own after a specified time .

6. End (TERMINATED): Indicates that the thread has finished executing .

State diagram of thread

1. The initial state

Realization Runnable Interfaces and inheritance Thread You can get a thread class ,new An example comes out , The thread is in its initial state .

2.1. Ready state

Ready state just means that you are qualified to run , The scheduler didn't pick you , You are always ready . Calling thread's start() Method , This thread is in ready state . Current thread sleep() Method end , Other threads join() end , Wait for user input to complete , A thread gets the object lock , These threads will also be ready . The current thread time slice is used up , Call the... Of the current thread yield() Method , The current thread is in ready state . After the thread in the lock pool gets the object lock , Get ready .

2.2. In-service state

The thread scheduler selects a thread from the runable pool as the current thread's state . This is also the only way for a thread to enter the running state .

3. Blocked state

The blocking state is that the thread is blocking into synchronized Keyword decorated method or code block ( Get the lock ) State of .

4. wait for

Threads in this state are not allocated CPU execution time , They wait to be awakened explicitly , Otherwise it will be in a state of waiting indefinitely .

5. Overtime waiting

Threads in this state are not allocated CPU execution time , But there is no need to wait indefinitely for other threads to wake up , They wake up automatically after a certain time .

6. Termination status

Thread run() When the method is complete , Or the main thread 's main() When the method is complete , We think it's over . This thread object may be alive , however , It's not a single thread anymore . Once the thread is terminated , Can't be reborn . Called on a terminated thread start() Method , Will throw out java.lang.IllegalThreadStateException abnormal .

7. Waiting in line

call obj Of wait(), notify() Before the method , Must obtain obj lock , That is to say, it must be written in synchronized(obj) In the code snippet . Steps and figures related to waiting queue

  1. Threads 1 Get objects A Lock of , Using object A.

  2. Threads 1 Call object A Of wait() Method .

  3. Threads 1 Release object A Lock of , And immediately into the waiting line .

  4. The object in the lock pool competes for the object A Lock of .

  5. Threads 5 Get object A Lock of , Get into synchronized block , Use object A.

  6. Threads 5 Call object A Of notifyAll() Method , Wake up all threads , All threads enter the synchronization queue . If a thread 5 Call object A Of notify() Method , Then wake up a thread , I don't know who will wake up , The awakened thread enters the synchronization queue .

  7. notifyAll() The way is synchronized end , Threads 5 Release object A Lock of .

  8. Threads competing for object lock in synchronous queue , But threads 1 I don't know when I can get it .

Synchronization queue status

The current thread wants to call the object A When synchronizing methods , Find out who A The lock of is owned by another thread , At this time, the current thread enters the synchronization queue . in short , All threads in the synchronization queue want to compete for the object lock .

When a thread 1 By another thread 2 Wake up ,1 The thread enters the synchronization queue , To fight for the lock . Synchronous queue is a concept only existed in synchronous environment , An object corresponds to a synchronization queue .

Comparison of several methods

Thread.sleep(long millis), This method must be called by the current thread , The current thread enters TIMED_WAITING state , But don't release the object lock ,millis After that, the thread automatically wakes up and enters the ready state . effect : The best way to give other threads the chance to execute .

Thread.yield(), This method must be called by the current thread , The current thread gave up getting CPU Time slice , But don't release lock resources , From running state to ready state , Give Way OS Choose thread again . effect : Let threads of the same priority take turns to execute , But there is no guarantee that it will take turns . In practice, there is no guarantee that yield() To achieve the purpose of concession , Because the compromised thread may be selected again by the thread scheduler .Thread.yield() It won't cause obstruction . The method and sleep() similar , It's just that the user can't specify how long to pause .

t.join()/t.join(long millis), Call other threads in the current thread t Of join Method , The current thread enters WAITING/TIMED_WAITING state , The current thread will not release the object lock already held . Threads t Done or millis Time out , The current thread is in ready state .

obj.wait(), The current thread calls the object's wait() Method , The current thread releases the object lock , Enter the waiting queue . rely on notify()/notifyAll() Wake up or wait(long timeout) timeout Time to wake up automatically .

obj.notify() Wake up a single thread waiting on this object monitor , Choice is arbitrary .notifyAll() Wakes up all threads waiting on this object monitor .


  1. [TTS] AIX - & gt; Linux -- Based on RMAN (real environment)
  2. 为什么学编程大部分人选Java编程语言?
  3. Redis 高可用篇:你管这叫 Sentinel 哨兵集群原理
  4. redis 为什么把简单的字符串设计成 SDS?
  5. [TTS] transfer table space AIX - & gt; Linux based on RMAN
  6. Linux 网卡数据收发过程分析
  7. Redis 高可用篇:你管这叫 Sentinel 哨兵集群原
  8. Redis 6.X Cluster 集群搭建
  9. [TTS] transfer table space AIX ASM - & gt; Linux ASM
  10. [TTS] transfer table space Linux ASM - & gt; AIX ASM
  11. 高性能通讯框架——Netty
  12. Brief introduction and test of orchestrator, a high availability management tool for MySQL
  13. [TTS] transfer table space Linux - & gt; AIX based on RMAN
  14. A love diary about http
  15. [rocketmq source code analysis] in depth message storage (3)
  16. Implementation of service configuration center with spring cloud + Nacos (Hoxton version)
  17. SiCp: abstraction of construction process -- object oriented explanation
  18. springboot网上点餐系统
  19. 【SPM】oracle如何固定执行计划
  20. 用好HugePage,告别Linux性能故障
  21. 3 W word long text, java basic interview questions! It's amazing!!!
  22. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  23. Win10 uninstall mysql5.7
  24. CentOS下dotnet Core使用HttpWebRequest进行HTTP通讯,系统存在大量CLOSE_WAIT连接问题的分析,已解决。
  25. MySQL batch insert, how not to insert duplicate data?
  26. K8s cronjob application example
  27. Unconventional method, easy to deal with Oracle database critical exception
  28. How to use sqlplus - prelim in Oracle hang
  29. How to search Oracle official documents in full text
  30. Install mysql8.0 on win10
  31. Oracle OCR的备份与恢复
  32. Oracle kill session相关问题
  33. 《Oracle DBA工作笔记》第二章 常用工具和问题分析
  34. Oracle回收站及flashback drop
  35. Hand in hand to teach you to write a spring IOC container
  36. Exception in Java (1) - basic concept
  37. 3w 字长文爆肝 Java 基础面试题!太顶了!!!
  38. Error 2059 when Navicat connects to win10 mysql8.0
  39. Parameter reminder causing Oracle Performance jitter
  40. 「技术分享」Java线程状态间的互相转换看这个就行了
  41. 国产控件短平快,在Java中以编程形式将 XML 转为 Excel
  42. Oracle RAC high availability failure of risk alert
  43. Process scheduling bugs in running oracle on small computers
  44. Oracle memory over consumption risk alert
  45. 【硬核】23种设计模式娓娓道来,助你优雅的编写出漂亮代码!
  46. springboot整合spring security最完整,只看这一篇就够了
  47. Oracle SQL monitor
  48. Using Bifrost to realize data synchronization of MySQL
  49. Reveal the principle of Oracle database truncate
  50. Read this article, Oracle SQL optimization article does not need to read!
  51. IntelliJ IDEA 2021.1 破解,IDEA 2021.1激活破解,激活持续更新
  52. Kafka performance: why so fast?
  53. Two high frequency design interview questions: how to design HashMap and thread pool
  54. Why most people choose Java programming language to learn programming?
  55. Redis high availability: you call this sentinel cluster principle
  56. Why does redis design simple strings as SDS?
  57. Analysis of data sending and receiving process of Linux network card
  58. Redis high availability: what do you call sentinel cluster
  59. Redis 6. X cluster construction
  60. Netty: a high performance communication framework