当前位置:网站首页>Analysis of synchronized
Analysis of synchronized
2022-06-13 06:23:00 【bearstwu】
synchronized The most important function in threads is to protect thread safety , It is very easy to call data at the same time when the thread is processing , The main problem at this time is that the thread has been processed twice , But in both cases , In fact, I only got the answer once .
So in order to solve this problem , When dealing with a calculation problem that occurs in multiple threads at the same time, you must directly avoid other threads from getting the variable or formula . So in java More implemented in previous versions Volatile. There is a visibility problem when using . It is similar to the case that the hospital is now full of patients , If the ambulance is not visible, it may still bring patients to the hospital , But after arriving, I will find that the hospital is full of patients ,,Volatile This feature is well realized , But there are problems ,Volatile The scope of application is too small
1. Writing to a variable does not depend on the current value of the variable , Or you can make sure that only a single thread updates the value of the variable .
2. This variable is not included in invariants with other variables .
So let me list here volatile and synchronized The difference between
- volatile The essence is to tell jvm Current variable in register ( The working memory ) The value in is uncertain , Need to read from main memory ; synchronized Lock the current variable , Only the current thread can access this variable , Other threads blocked .
- volatile Can only be used at variable level ;synchronized You can use the 、 Method 、 And class level
- volatile Only variable modification visibility can be achieved , Atomicity is not guaranteed ; and synchronized It can ensure the visibility and atomicity of the variables
- volatile No thread blocking ;synchronized May cause thread blocking .
- volatile Tagged variables are not optimized by the compiler ;synchronized Tagged variables can be optimized by the compiler
public class AccountingSyncBad implements Runnable{
static int i=0;
public synchronized void increase(){
i++;
}
@Override
public void run() {
for(int j=0;j<1000000;j++){
increase();
}
}
public static void main(String[] args) throws InterruptedException {
//new New examples
Thread t1=new Thread(new AccountingSyncBad());
//new New examples
Thread t2=new Thread(new AccountingSyncBad());
t1.start();
t2.start();
//join meaning : Current thread A wait for thread After the thread is terminated, it can start from thread.join() return
t1.join();
t2.join();
System.out.println(i);
}
}
In this case, thread safety can be guaranteed , But if you delete in the third line synchronized Words , The result is hard to be 20000, The greater probability of occurrence is less than 20000 but greater than or equal to 10000 The number of
边栏推荐
- [MySQL] basic knowledge review
- Recent problems
- Wechat applet custom tabbar (session customer service) vant
- Free screen recording software captura download and installation
- 端午安康,使用祝福话语生成词云吧
- Multiple reception occurs in the uniapp message delivery
- Echart histogram: stack histogram value formatted display
- Applet export (use) public function, public data
- Alibaba cloud OSS file download cannot be resumed at a breakpoint
- ADB shell content command debug database
猜你喜欢
杨辉三角形详解
Super model logo online design and production tool
1+1 > 2, share creators can help you achieve
MFS详解(七)——MFS客户端与web监控安装配置
USB debugging assistant (20191028)
The web server failed to start Port 7001 was already in use
AI realizes "Resurrection" of relatives | old photo repair | old photo coloring, recommended by free app
微信小程序:点击事件获取当前设备信息(基础)
SSM框架整合--->简单后台管理
华为开发者认证与DevEco Studio编译器下载
随机推荐
App performance test: (I) startup time
Dynamic link library nesting example
推荐扩容工具,彻底解决C盘及其它磁盘空间不够的难题
Echart histogram: stacked histogram displays value
[2022 college entrance examination season] what I want to say as a passer-by
Applet disable native top
Detailed explanation of PHP distributed transaction principle
MFS详解(五)——MFS元数据日志服务器安装与配置
The boys x pubgmobile linkage is coming! Check out the latest game posters
[one · data 𞓜 simple implementation of the leading two-way circular linked list]
App performance test: (IV) power
Failed to extract manifest from apk: processexception:%1 is not a valid Win32 Application.
不在以下合法域名列表中,微信小程序解决办法
Intelligent digital asset management helps enterprises win the post epidemic Era
Commit specification
Echart柱状图:堆叠柱状图显示value
Introduction to USB learning (I) -- Dongfeng night flower tree
欧姆龙平替国产大货—JY-V640半导体晶元盒读写器
微信小程序:全局状态变量的使用
【MySQL】基础知识小复习