线程结束时,工作内存会同步到主内存吗,请详细说明一下

w1215142854 2018-07-11 03:54:49
java规范中定义了8中工作内存和主内存之间交互操作,但是没有说明什么时候会触发这些操作,我想知道在线程结束时,工作内存会同步到主内存吗,如果会的话又是如何同步的?
...全文
929 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Nihility/ 2018-12-03
  • 打赏
  • 举报
回复 1
这个oracle的官方文档也可以参考一下 https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html
Nihility/ 2018-12-03
  • 打赏
  • 举报
回复 1
引用
After we exit a synchronized block, we release the monitor, which has the effect of flushing the cache to main memory, so that writes made by this thread can be visible to other threads.
查了一下文档,可以从这里看出来,严格的讲,并不是退出方法的时候,进行同步的。是在退出同步代码块,且释放了监视器的情况下,监视器负责将寄存器中的缓存,写入到主内存,这样其他线程就可以访问到你写入的数据。 援引来自: https://en.wikipedia.org/wiki/Java_memory_model http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
Nihility/ 2018-12-03
  • 打赏
  • 举报
回复
引用 9 楼 干净的句号 的回复:
这个问题也困扰了我很久,援引 http://tutorials.jenkov.com/java-concurrency/java-memory-model.html
The values stored in the cache memory is typically flushed back to main memory when the CPU needs to store something else in the cache memory. The CPU cache can have data written to part of its memory at a time, and flush part of its memory at a time. It does not have to read / write the full cache each time it is updated. Typically the cache is updated in smaller memory blocks called "cache lines". One or more cache lines may be read into the cache memory, and one or mor cache lines may be flushed back to main memory again.
当cache需要存储其它内容时就将缓存写回到主内存,而且可能是写回部分。总之,什么时候写回主内存是不确定的,由cpu决定,可能也存在指令指明立刻写回主内存,如果没有指令,那就取决于cache是否够用。
有没有什么官方的解释?
wangjun5159 2018-11-29
  • 打赏
  • 举报
回复
这个问题也困扰了我很久,援引 http://tutorials.jenkov.com/java-concurrency/java-memory-model.html
The values stored in the cache memory is typically flushed back to main memory when the CPU needs to store something else in the cache memory. The CPU cache can have data written to part of its memory at a time, and flush part of its memory at a time. It does not have to read / write the full cache each time it is updated. Typically the cache is updated in smaller memory blocks called "cache lines". One or more cache lines may be read into the cache memory, and one or mor cache lines may be flushed back to main memory again.

当cache需要存储其它内容时就将缓存写回到主内存,而且可能是写回部分。总之,什么时候写回主内存是不确定的,由cpu决定,可能也存在指令指明立刻写回主内存,如果没有指令,那就取决于cache是否够用。
叫我放猪之人 2018-08-13
  • 打赏
  • 举报
回复
应该会同步的,可以将工作内存对应虚拟机栈,主内存对应于堆,虚拟机栈上面持有的是堆上面实例对象的引用,对一个引用做操作会反应到堆上面的,只不过是Store(存储)和Write(写入)的时机问题
w1215142854 2018-08-13
  • 打赏
  • 举报
回复
引用 5 楼 xinzhongtianxia 的回复:
[quote=引用 4楼我是你的主体 的回复:][quote=引用 3 楼 lrx2011 的回复:]
会,工作内存中的数据不同步到主内存,那你数据怎么同步

我也只这样认为的,但这只是我们想的,这个有什么理论依据吗,在jvm里是如何体现的?[/quote]会,不需要理论依据,不同步的话,设计分身就有问题了[/quote] 你这是放屁呢,不知道就不要回答
  • 打赏
  • 举报
回复
引用 4楼我是你的主体 的回复:
[quote=引用 3 楼 lrx2011 的回复:]
会,工作内存中的数据不同步到主内存,那你数据怎么同步

我也只这样认为的,但这只是我们想的,这个有什么理论依据吗,在jvm里是如何体现的?[/quote]会,不需要理论依据,不同步的话,设计分身就有问题了
w1215142854 2018-07-11
  • 打赏
  • 举报
回复
引用 3 楼 lrx2011 的回复:
会,工作内存中的数据不同步到主内存,那你数据怎么同步

我也只这样认为的,但这只是我们想的,这个有什么理论依据吗,在jvm里是如何体现的?
podd 2018-07-11
  • 打赏
  • 举报
回复
会,工作内存中的数据不同步到主内存,那你数据怎么同步
w1215142854 2018-07-11
  • 打赏
  • 举报
回复
引用 1 楼 lrx2011 的回复:
https://blog.csdn.net/lovetea99/article/details/53375649

这个帖子我看过,里面没有说明“线程结束时,工作内存中的数据会不会同步到主内存”
podd 2018-07-11
  • 打赏
  • 举报
回复
https://blog.csdn.net/lovetea99/article/details/53375649

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧