当前位置:网站首页>认识线程
认识线程
2022-07-01 22:26:00 【InfoQ】
认识线程
线程的状态
- NEW,新建状态:线程被创建出来,但尚未启动时的线程状态
- RUNNABLE,就绪状态:表示线程可以运行的状态,它可能正在运行,或者是在排队等待操作系统分配 CPU 资源;
- BLOCKED,阻塞等待锁的线程状态:表示处于阻塞状态的线程,正在等待锁释放,比如等待执行 synchronized 代码块或者使用 synchronized 标记的方法,但此时已经被其他线程执行,所以处于阻塞等待。
- WAITING,等待状态:一个线程正在等待另一个线程执行某个特定的动作,这个线程就处于等待状态。比如一个线程调用了 Object.wait() ,那它在等待另一个线程调用 Object.notify() 或 Object.notifyAll() 方法时所处的状态,称之为等待状态。
- TIMED_WAITING,超时等待状态:当进行等待状态时,会设置一个超时时间,当等待时长超过规定的时间后,线程所处的状态就称为超时等待状态。比如 Object.wait(long timeout) 和 Thread.join(long timeout) 等方法,就会让线程有可能进入此状态。
- TERMINATED,终止状态:表示线程已经执行完成。
public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED
}
线程的工作模式
- 首先创建线程,此时线程处于NEW状态。
- 指定线程需要执行的业务方法,此时线程处于RUNNABLE状态
- 线程执行过程中,如果遇到有同步代码块,并且其他线程先一步执行,则当前线程则会进入BLOCKED状态。待其他线程执行完成同步代码块之后,当前线程会继续等待。
- 如果线程中代码有调用Object.wait()方法或者Thread.join()方法时,线程会进入WAITING状态,并且当有设置超时时间时,则线程会进入TIMED_WAITING等待状态。直到有其他线程执行了notify() 或 notifyAll() 方法,当前线程才会被唤醒,继续执行代码。
- 最后线程方法执行完成,线程也就变成了TERMINATED状态。
边栏推荐
- SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
- Hide the creation and use of users
- 业务可视化-让你的流程图'Run'起来
- window安装wsl(二)
- 力扣 710. 黑名单中的随机数
- 数字化转型道阻且长,如何迈好关键的第一步
- 实在RPA:银行数字化,业务流程自动化“一小步”,贷款审核效率“一大步”
- Use three JS realize the 'ice cream' earth, and let the earth cool for a summer
- internal field separator
- Explain the use of locksupport in detail
猜你喜欢

Advanced skills of testers: a guide to the application of unit test reports

Digital currency: far-reaching innovation

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received

SAP intelligent robot process automation (IRPA) solution sharing

Detailed explanation of twenty common software testing methods (the most complete in History)

"Trust machine" empowers development

Preparation of functional test report

Multiple smart pointers

Appium automation test foundation - appium installation (I)

赵福全:短期解决保供,长期要打造安全、高效有韧性的供应链
随机推荐
【嵌入式系统课设】单个按键控制LED灯
Design of ESP automatic download circuit
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
Jielizhi, production line assembly link [chapter]
思科--高可用和高可靠网络考试
rviz打开后如何显示实时2D地图
Talk about what parameters ZABBIX monitors
Preparation of functional test report
Contents of other parts of the map
Reprint CSDN article operation
Tcpdump command usage details
第三方验收测试有什么好处?专业第三方软件测试机构推荐
软考信息系统项目管理师_整理的十大管理过程的简写帮助记忆背诵---软考高级之信息系统项目管理师054
攻防演习防御体系构建之第三篇之建立实战化的安全体系
[target tracking] | single target tracking indicator
ECMAScript 2022 was officially released. Have you heard about it?
91. (cesium chapter) cesium rocket launch simulation
[MySQL] index classification
Congratulations on the release of friends' new book (send welfare)
OpenVINO 模型性能评估工具—DL Workbench