当前位置:网站首页>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
边栏推荐
- QT interface optimization: QT border removal and form rounding
- Logical volume creation and expansion
- Unity_代码方式添加绑定按钮点击事件
- 面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
- Chapter 7 of JVM series -- bytecode execution engine
- SVN详细使用教程
- Outsourcing for four years, abandoned
- Use of ansible and common modules
- QT actual combat: Yunxi calendar
- QT Detailed explanation of pro file
猜你喜欢
ASEMI整流模块MDQ100-16在智能开关电源中的作用
Svn detailed use tutorial
TLC5615 based multi-channel adjustable CNC DC regulated power supply, 51 single chip microcomputer, including proteus simulation and C code
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
3、 Gradient descent solution θ
【Servlet】Servlet 详解(使用+原理)
随机推荐
你還不知道責任鏈模式的使用場景嗎?
Solve the problem of SSH configuration file optimization and slow connection
ArrayList collection basic usage
想要成为架构师?夯实基础最重要
2-Go变量操作
Epolloneshot event of epoll -- instance program
1-初识Go语言
MySQL报错packet out of order
如何打开Win10启动文件夹?
数组模拟队列进阶版本——环形队列(真正意义上的排队)
三、梯度下降求解最小θ
[untitled]
Usage of BC
PCIe X1 插槽的主要用途是什么?
3、 Gradient descent solution θ
QT interface optimization: QT border removal and form rounding
8.4 循环神经网络从零实现
【Proteus仿真】自动量程(范围<10V)切换数字电压表
Chapter 7 of JVM series -- bytecode execution engine
Use of ansible and common modules