当前位置:网站首页>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
边栏推荐
- 一款不错的工具:aardio
- Basic regular expression
- SVN详细使用教程
- I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
- capacitance
- Find daffodils - for loop practice
- Achievements in science and Technology (21)
- Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
- Usage of BC
- 数组模拟队列进阶版本——环形队列(真正意义上的排队)
猜你喜欢
51 Single Chip Microcomputer Design of traffic light system (with Proteus simulation, C program, schematic diagram, PCB, thesis and other complete data)
A blog allows you to learn how to write markdown on vscode
直流可调稳压电源的Proteus仿真设计(附仿真+论文等资料)
51 MCU + LCD12864 LCD Tetris game, proteus simulation, ad schematic diagram, code, thesis, etc
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
3、 Gradient descent solution θ
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
一篇博客让你学会在vscode上编写markdown
【NLP】HMM隐马尔可夫+维特比分词
随机推荐
抑郁症治疗的进展
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
8.5 循环神经网络简洁实现
The initial C language framework is suitable for review and preliminary understanding
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
555定时器+74系列芯片搭建八路抢答器,30s倒计时,附Proteus仿真等
ASEMI整流模块MDQ100-16在智能开关电源中的作用
AT89C52单片机的频率计(1HZ~20MHZ)设计,LCD1602显示,含仿真、原理图、PCB与代码等
Usage of BC
ArrayList collection basic usage
OpenFaaS实战之四:模板操作(template)
8.3 语言模型与数据集
想要成为架构师?夯实基础最重要
Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)
Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
【NLP】HMM隐马尔可夫+维特比分词