当前位置:网站首页>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
边栏推荐
- Can deleted wechat friends be recovered? How to retrieve wechat friends after they are accidentally deleted
- Fiddler过滤会话
- Do you know the five GoLand shortcuts to improve efficiency?
- From web2 to Web3, ideology also needs to change
- VBA divides strings, stores them in an array, writes them to a file, and saves them
- Simple file upload
- ahk常用函数
- 全链路追踪 & 性能监控工具 SkyWalking 实战
- Fabric.js 锁定背景图,不受缩放和拖拽的影响
- Fiddler配置
猜你喜欢

Basic use cases for jedis

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

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

ADB is not an internal or external command, nor is it a runnable program or batch file

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

Palm detection and finger counting based on OpenCV

卷起来,突破35岁焦虑,动画演示CPU记录函数调用过程,进互联大厂如此简单

Fiddler为测试做什么
![[quick code] define the new speed of intelligent transportation with low code](/img/cd/da8cf959200dba8eeab6c8eccc5635.jpg)
[quick code] define the new speed of intelligent transportation with low code

提高效率的 5 个 GoLand 快捷键,你都知道吗?
随机推荐
消除业务代码中if....else的五种方式
From web2 to Web3, ideology also needs to change
Pytorch Foundation (I) -- anaconda and pytorch installation
Calling subroutines from other modules in VBA - call a subroutine from a different module in VBA
Only by truly finding the right way to land and practice the meta universe can we ensure the steady and long-term development
Web2 到 Web3,意识形态也需要发生改变
Comply with medical reform and actively layout -- insight into the development of high-value medical consumables under the background of centralized purchase 2022
顺应医改,积极布局——集采背景下的高值医用耗材发展洞察2022
Nat. Rev. Drug Discov. | AI在小分子药物发现中的应用:一个即将到来的浪潮?
从零开始,如何拥有自己的博客网站【华为云至简致远】
Basic use cases for jedis
[web security self-study] section 1 building of basic Web Environment
用代码设计药物:我们到了吗?
Do you know the five GoLand shortcuts to improve efficiency?
Nat. Commun. | 用于加速发现抗生素抗性基因的知识整合和决策支持
智慧景区视频监控 5G智慧灯杆网关组网综合杆
Cannot locate a 64-bit Oracle Client library:The specified module could not be found.
Enroulez - vous, brisez l'anxiété de 35 ans, l'animation montre le processeur enregistrer le processus d'appel de fonction, entrer dans l'usine d'interconnexion est si simple
“禁塑令”下,中宝新材深挖可降解塑料,港交所买单吗?
[untitled]