当前位置:网站首页>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单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
- ASEMI超快恢复二极管与肖特基二极管可以互换吗
- Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
- MDS55-16-ASEMI整流模块MDS55-16
- UML project example -- UML diagram description of tiktok
- One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
- 外包干了四年,废了...
- Branch statement of process control
- 1 - first knowledge of go language
- c语言在结构体传参时参数压栈问题
猜你喜欢

外包干了四年,废了...

3、 Gradient descent solution θ

Design of single chip microcomputer Proteus for temperature and humidity monitoring and alarm system of SHT11 sensor (with simulation + paper + program, etc.)

详解TCP的三次握手
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

Matlab Simulink modeling and design of single-phase AC-AC frequency converter, with MATLAB simulation, PPT and papers

we引用My97DatePicker 实现时间插件使用

QT Detailed explanation of pro file

阿里研发三面,面试官一套组合拳让我当场懵逼

【NLP】HMM隐马尔可夫+维特比分词
随机推荐
DVWA之暴力破解(Brute Force)Low-->high
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
一个月把字节,腾讯,阿里都面了,写点面经总结……
编程哲学——自动加载、依赖注入与控制反转
Achievements in science and Technology (21)
解决computed属性与input的blur事件冲突问题
Four ways of SSH restricting login
A blog allows you to learn how to write markdown on vscode
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
capacitance
电子秤称重系统设计,HX711压力传感器,51单片机(Proteus仿真、C程序、原理图、论文等全套资料)
3、 Gradient descent solution θ
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
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
Frame synchronization implementation
PCIe X1 插槽的主要用途是什么?
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
ASEMI整流模块MDQ100-16在智能开关电源中的作用
阿里研发三面,面试官一套组合拳让我当场懵逼