当前位置: 首页 > news >正文

java获取内存变量,java – 易失性变量,从主内存刷新/读取

官方说明说,那

Writing to a volatile field has the same memory effect as a monitor release, and reading from a volatile field has the same memory effect as a monitor acquire.

Effectively, the semantics of volatile have been strengthened substantially, almost to the level of synchronization. Each read or write of a volatile field acts like “half” a synchronization, for purposes of visibility.

这是否意味着,对volatile变量的任何写入都会使执行线程将其缓存刷新到主内存中,并且每次从volatile字段读取都会使线程从主内存重新读取其变量?

我问,因为同一文本包含此声明

Important Note: Note that it is important for both threads to access the same volatile variable in order to properly set up the happens-before relationship. It is not the case that everything visible to thread A when it writes volatile field f becomes visible to thread B after it reads volatile field g. The release and acquire have to “match” (i.e., be performed on the same volatile field) to have the right semantics.

这句话让我很困惑.我知道对于使用synchronized语句定期获取和释放不是这样 – 如果某个线程释放任何监视器,那么它所做的所有更改都会明显地变为所有其他线程(更新:实际上不是真的 – 请注意最佳答案). stackoverflow上甚至有一个question about it.然而据说无论出于何种原因,情况并非如此.我无法想象发生在事先保证的任何实现,它不会使其他线程看到更改,这些线程不会读取相同的volatile变量.至少想象一个实现,它与前两个引号并不矛盾.

此外,在发布这个问题之前,我做了一些研究,例如this article,其中包含这个句子

After executing these instructions, all writes are visible to all other threads through cache subsystem or main memory.

提到的指令是在写入易失性字段时发生的指令.

那么这个重要的音符应该是什么意思?或者我错过了什么?或许这个说明完全错了?

回答?

在进行了一些更多的研究之后,我只能在官方文档中找到关于volatile字段及其对非易失性字段更改的影响的声明:

Using volatile variables reduces the risk of memory consistency errors, because any write to a volatile variable establishes a happens-before relationship with subsequent reads of that same variable. This means that changes to a volatile variable are always visible to other threads. What’s more, it also means that when a thread reads a volatile variable, it sees not just the latest change to the volatile, but also the side effects of the code that led up the change.

我不知道这是否足以得出结论,这种情况发生在关系只保证读取相同的volatile的线程之前.所以现在我只能总结一下结果是不确定的.

但实际上,我建议考虑线程A在写入易失性字段时所做的更改,只有当线程B读取相同的volatile字段时才能保证线程B可见.官方消息来源的上述引用强烈暗示.

相关文章:

  • java二维函数怎么放到表里,调用函数传递二维数组
  • jsonrpc php使用,基于php的json rpc原理及应用
  • php正则表达式变量替换,js正则表达式replace替换变量方法
  • php ab webbance,Apache的ab工具实例详解
  • 腾讯的PHP框架,腾讯音视频 TRTC
  • php定时刷新token,PHP定时任务获取微信access_token的方法实例分享
  • java机房上机模拟系统,机房上机安排管理系统,基于B/S模式下的JAVA系统
  • java引用技术,Java 8 方法引用
  • MATLAB简单绘图命令,简单的matlab绘图命令
  • matlab模糊控制移值到c,C/C++实现模糊控制,借助MATLAB辅助设计和fis.c文件 | 学步园...
  • matlab 稳定系统,matlab分析系统的稳定性
  • PHP的continue语句,PHP中continue语句的应用
  • php判断区间数字,如何快速判断数字在那个区间?
  • 有限差分matlab工具箱,FDTD(时域有限差分法)算法的Matlab源程序
  • matlab实现主机通信,设置主机模型和目标模型之间的通信
  • 《网管员必读——网络组建》(第2版)电子课件下载
  • 【从零开始安装kubernetes-1.7.3】2.flannel、docker以及Harbor的配置以及作用
  • Docker入门(二) - Dockerfile
  • gcc介绍及安装
  • gulp 教程
  • HTTP中的ETag在移动客户端的应用
  • in typeof instanceof ===这些运算符有什么作用
  • Java 最常见的 200+ 面试题:面试必备
  • JavaScript设计模式系列一:工厂模式
  • maya建模与骨骼动画快速实现人工鱼
  • MQ框架的比较
  • React Transition Group -- Transition 组件
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • 初识 beanstalkd
  • 微信小程序设置上一页数据
  • 吴恩达Deep Learning课程练习题参考答案——R语言版
  • 小李飞刀:SQL题目刷起来!
  • scrapy中间件源码分析及常用中间件大全
  • 大数据全解:定义、价值及挑战
  • ​DB-Engines 12月数据库排名: PostgreSQL有望获得「2020年度数据库」荣誉?
  • #Java第九次作业--输入输出流和文件操作
  • (3)选择元素——(17)练习(Exercises)
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (排序详解之 堆排序)
  • (原創) 如何刪除Windows Live Writer留在本機的文章? (Web) (Windows Live Writer)
  • ./configure,make,make install的作用
  • .NET CLR Hosting 简介
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET Core/Framework 创建委托以大幅度提高反射调用的性能
  • .NET 线程 Thread 进程 Process、线程池 pool、Invoke、begininvoke、异步回调
  • .net对接阿里云CSB服务
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .NET面试题(二)
  • @GlobalLock注解作用与原理解析
  • [ C++ ] STL_stack(栈)queue(队列)使用及其重要接口模拟实现
  • [2024最新教程]地表最强AGI:Claude 3注册账号/登录账号/访问方法,小白教程包教包会
  • [Angular] 笔记 7:模块
  • [BT]BUUCTF刷题第8天(3.26)
  • [CISCN2019 华东北赛区]Web2