当前位置:网站首页>Thread synchronization, life cycle
Thread synchronization, life cycle
2022-04-23 14:45:00 【clear0217】
Thread synchronization
So called synchronization , When a function call is issued , Before we get results , The call does not return , At the same time, other threads cannot call this method .
- The need for thread synchronization
Ensure multi-threaded secure access to competing resources .
To prevent multiple threads from accessing a data object , Damage to data .
- Thread synchronization principle
In the above example, we can see that when we implement thread synchronization , Used synchronized keyword . The keyword modification is equivalent to locking the method . When a thread accesses a method, it needs to obtain a lock first , Then the method can be executed . here , If other threads come , Found that the lock has been occupied , Wait at the method call , Until the lock is released , Then compete for the lock . Gets the lock , Then execute the method , otherwise , Continue to wait for .
Thread lock
Every Java Object has a built-in lock , There is one and only one .
When the program executes to a non static synchronized Synchronizing methods ,( If the lock is not occupied by other threads ) Automatically obtain the lock of the object to which the method belongs .
It's not just a method that can be modified to synchronize methods , You can also lock code blocks .
When the program is executed , The lock is released when the synchronization block or synchronization method is exited .
【 Be careful 】
You don't have to synchronize all the methods in the class , Synchronize as needed .
Threads sleep When sleeping , The lock it holds will not release .
Threads can acquire multiple locks .
Synchronization damages concurrency , You should minimize the scope of synchronization .
Lock
Lock and synchronized To solve the problem of thread synchronization ,Lock be relative to synchronized More functions 、 More flexible use 、 More object oriented .
【 Use 】
private Lock lock = new ReentrantLock();
public void take(){
lock.lock();
// The synchronization part
lock.unlock();
}
There are five states in the life cycle of a thread : newly build 、 be ready 、 function 、 Blocking 、 Death
newly build : use new Statement creation complete
be ready : perform start after
function : Occupy CPU Time
Blocking : Yes wait sentence 、 Yes sleep Statement and waiting for an object lock , Waiting for input
End : After execution run() Method
The difference between a process and a thread
Processes have separate code and data spaces , The cost of inter process switching is high .
Multiple threads of the same process share process code and data space , Only separate thread stacks are retained 、 Program counter , Low switching cost .
Multiprocesses are multiple independent tasks that can be seen running in parallel in the operating system .
Multithreading refers to multiple parallel executing programs in the same process .
版权声明
本文为[clear0217]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231426296186.html
边栏推荐
- 51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
- Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
- Use of ansible and common modules
- 外包干了四年,废了...
- ASEMI三相整流桥和单相整流桥的详细对比
- Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
- LotusDB 设计与实现—1 基本概念
- QT interface optimization: double click effect
- AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
- 【无标题】
猜你喜欢
随机推荐
Epolloneshot event of epoll -- instance program
GIS数据处理-cesium中模型位置设置
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
2-Go变量操作
Upgrade of openssh and modification of version number
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
Some little records~
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Use of ansible and common modules
51单片机的直流电机PWM调速控制系统(附Proteus仿真+C程序等全套资料)
单相交交变频器的Matlab Simulink建模设计,附Matlab仿真、PPT和论文等资料
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
Epoll's et, lt working mode -- example program
如何打开Win10启动文件夹?
On the insecurity of using scanf in VS
Don't you know the usage scenario of the responsibility chain model?
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码