当前位置:网站首页>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
边栏推荐
猜你喜欢

隐形马尔可夫模型及其训练(一)

Palm detection and finger counting based on OpenCV

Calling subroutines from other modules in VBA - call a subroutine from a different module in VBA

消除业务代码中if....else的五种方式

Carry forward the past and forge ahead into the future, multiple residuals | densenet (II)

postman切换主题

Docker安装Redis镜像详细步骤(简单易懂,适合新手快速上手)

Detailed steps for installing redis image in docker (easy to understand, suitable for novices to get started quickly)

Comply with medical reform and actively layout -- insight into the development of high-value medical consumables under the background of centralized purchase 2022

Take you to play completablefuture asynchronous programming
随机推荐
如何用Pygame制作简单的贪吃蛇游戏
rmq 2333
Download and install pycharm integrated development environment [picture]
Web2 到 Web3,意识形态也需要发生改变
靠,嘉立创打板又降价
MFC basic knowledge and course design ideas
VBA divides strings, stores them in an array, writes them to a file, and saves them
DAO 是如何实现去中心化治理的?
接口测试学习笔记
Hidden Markov model and its training (1)
【无标题】
Only by truly finding the right way to land and practice the meta universe can we ensure the steady and long-term development
Fabric.js 激活输入框
VBA judge whether the file exists and ask for the idea of file backup
postman常用断言
Fabric.js 锁定背景图,不受缩放和拖拽的影响
Basic use of pycharm
Have you ever written a line of efficient code that equals 20 lines of others?
互联网企业与芯片
From web2 to Web3, ideology also needs to change