【多线程】——volatile和synchronize关键字对比

本文探讨了并发编程中的关键问题,包括线程间通信的方法如wait(), notify(), notifyAll()等,以及共享内存和消息传递两种并发模型的区别。详细解释了Java中内存可见性的挑战,volatile关键字的作用以及synchronized关键字如何实现锁机制。

并发编程中的两个关键问题

     线程之间如何通信:wait() notify() notifyall()

     共享内存:隐式通信

     消息传递:显示通信

     线程之间如何通信

     在共享内存的并发模型中,同步是显示做的;synchronize

     在消息传递的并发模型中,由于消息的发送必须在消息接收之前,所以同步是隐式的。

定位内存可见性问题

       在java中所有的实例 静态域 数组都会存在堆内存里面 ,堆实在线程之间共享的

什么对象是内存共享,什么不是,主要的执行过程

 

      简述过程:线程A和线程B会分别存储一份共享变量的副本在自己本地的内存当中,当线程A去修改了共享变量的值,需要将他同步到主内存,然后线程B在去同步主内存的修改之后的结果。来保持一致

执行可见性的方面

1、对于声明了volatile的变量进行写操作的时候,JVM回想处理器发送一条lock前缀的指令,会把这个变量所在的缓存行的数据写会系统内存。

2、在多处理器的情况下,保证各个处理器缓存一致性的特点,就会实现缓存一致性的协议。每个处理器会看到总线上的缓存的值是不是过期了,当处理器发现自己缓存的值被修改之后,就会把它设置为失效的状态,然后重新去系统内存中读取值,来更新自己本地内存缓存的值

Synchronized :可重入锁、互斥性、可见性。

Volatile 可以做到原子性、可见性,不能做到复合操作的原子性。

多线程循环调用这个方法的时候

1、读取i的值

2、i+1

3、把i写入内存

一共有三个步骤,这三个步骤没有办法保证一致性。

 

Synchronized实现锁机制

 

修改的对象的时候,首先去尝试获得Monitor,如果获得成功然后执行,之后释放,如果没有获得Monitor,则会进入synchronized的队列当中,这个队列也会又出队列的操作等待区获得Monitor。然后实现锁

按需引入<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Ant Design Vue 纯HTML项目示例</title> <!-- 引入Ant Design Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-vue@3.2.21/dist/antd.css"> <!-- 引入Ant Design X Vue的CSS --> <link rel="stylesheet" href="https://unpkg.com/ant-design-x-vue@1.0.0/dist/antdx.css"> <!-- vue3引入 --> <script src="https://unpkg.com/dayjs/dayjs.min.js"></script> <script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekday.js"></script> <script src="https://unpkg.com/dayjs/plugin/localeData.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script> <script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script> <script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script> <!-- vue3 --> <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script> <!-- antdv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-vue@4.2.6/dist/antd.min.js"></script> <!-- antdxv --> <script src="https://cdn.jsdelivr.net/npm/ant-design-x-vue@1.2.7/dist/index.umd.min.js"></script> </head> <body> <div id="app"></div> <script> const { createApp, ref, computed } = Vue; const { Button } = antd; const { Bubble, XProvider } = antdx; createApp({ template: ` <AXProvider :theme="{ algorithm: myThemeAlgorithm, }"> <div :style="{ padding: &#39;24px&#39;, backgroundColor: bgColor, }"> UMD <AXBubble content="hello bubble"></AXBubble> <AButton type="primary" @click="setLightTheme">Light</AButton> <AButton type="primary" @click="setDarkTheme">Dark</AButton> </div> </AXProvider> `, setup() { const { theme } = antd; const bgColor = ref("white"); const myThemeAlgorithm = ref(theme.defaultAlgorithm); const setLightTheme = () => { myThemeAlgorithm.value = theme.defaultAlgorithm; bgColor.value = "white"; }; const setDarkTheme = () => { myThemeAlgorithm.value = theme.darkAlgorithm; bgColor.value = "#141414"; }; return { myThemeAlgorithm, bgColor, setLightTheme, setDarkTheme }; } }) .use(XProvider) .use(Button) .use(Bubble) .mount("#app"); </script> <style> .container { max-width: 1200px; margin: 24px auto; padding: 0 16px; } .search-form { margin-bottom: 24px; padding: 16px; background-color: #f5f5f5; border-radius: 4px; } .user-table { margin-top: 16px; } .mb-6 { margin-bottom: 24px; } .mt-2 { margin-top: 8px; } </style> </body> </html>
07-17
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值