摘要
为了避免Java应用程序中多个线程共享同一个资源时产生访问冲突,确保线程安全,采用同步机制为每个线程合理地分配访问资源。编写一个模拟火车站售票过程的Java应用程序,由4个线程完成100张火车票的出售,调用sleep方法查看非同步环境下每个线程访问资源的状况。分析多线程采用同步机制和非同步机制的实验给系统带来的影响。实验证明,借助同步机制能合理地为每个线程提供没有任何冲突的资源访问,使Java多线程程序获得更好的健壮性。
In order to avoid access conflicts when multiple threads in a Java application share the same resource and ensure thread safety,a synchronized mechanism is used to reasonably allocate access resources for each thread.We write a Java application that simulates the ticket sales process at the train station.The sale of100train tickets are completed by4threads,and the sleep method is called to check the status of each thread accessing resources in an asynchronous environment.The impacts of the experimental results of multi-threaded synchronization and non-synchronization mechanisms on the system are analyzed.Experiments show that the synchronization mechanism can reasonably provide each thread resource access without conflicts,which makes Java multi-threaded program execution more robust.
作者
陈益
王佩
CHEN Yi;WANG Pei(School of Science, Hubei University of Technology, Wuhan 430068,China)
出处
《软件导刊》
2018年第12期165-168,172,共5页
Software Guide
基金
湖北省教育厅人文社科项目(17Q067)
湖北工业大学校级教改项目(2015035)
关键词
JAVA多线程
访问资源
线程安全
同步机制
健壮性
Java multi-thread
access to resources
thread safety
synchronization mechanism
robustness