摘 要: Java多线程能够提高CPU利用效率,但也容易造成线程不安全、线程死锁等问题。本文详细介绍了Java 线程各状态之间的关系及其切换,并用实例展示了使用同步锁synchronized保证同一时刻只有一个线程操作同一资源, 使用wait()、notify()切换线程状态保证线程操作的前后顺序实现线程交互。理解Java线程各状态之间的关系及其切换, 能帮助用户在使用Java多线程的场景有效避免多线程带来的不安全问题。 |
关键词: 多线程;synchronized;线程交互 |
中图分类号: TP312
文献标识码: A
|
基金项目: 山西省重点资助教学改革项目(面向工程教育专业认证持续改进的课程设计实施方案研究,J2017017). |
|
Realization of Thread Interaction Based on Java Synchronized |
CUI Zheng,DUAN Liguo1,2
|
1.( 1.Taiyuan University of Technology, Taiyuan 030024, China;2. 2.Pingshun County Power Supply Company, Shanxi Power Company, Changzhi 047400, China)
|
Abstract: Java multi-threading can improve CPU utilization,but it can also cause problems such as thread insecurity and thread deadlock.This paper introduces in detail the relationship between Java thread states and their transitions.Java synchronized ensures that only one thread operates the same resource at the same time,using wait() and notify() to switch the thread state to ensure that thread interactions are implemented in the context of thread operations.Understanding the relationship between the states of Java threads and their switching can help users avoid effectively the unsafe problems of multi-threading in Java multi-threaded scenarios. |
Keywords: multi-threading;synchronized;thread interaction |