- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
写入和读取 volatile 字段分别防止在 volatile 字段之前和之后读取/写入的重新排序。写入 volatile 变量之前的变量读/写不能被重新排序为在它之后发生,并且在从 volatile 变量读取之后的读/写不能被重新排序为发生在它之前。但是这个禁令的范围是什么?据我了解,volatile 变量只能在使用它的 block 内防止重新排序,对吗?
为了清楚起见,让我举一个具体的例子。假设我们有这样的代码:
int i,j,k;
volatile int l;
boolean flag = true;
void someMethod() {
int i = 1;
if (flag) {
j = 2;
}
if (flag) {
k = 3;
l = 4;
}
}
显然,写入 l
会阻止写入 k
的重新排序,但它会阻止写入 i
和 的重新排序吗j
相对于 l
?换句话说,可以在写入 l
之后写入 i
和 j
吗?
更新 1
感谢大家花时间回答我的问题 - 我很感激。问题是你回答错了问题。我的问题是关于范围,而不是关于基本概念。问题基本上是编译器在代码中保证与 volatile 字段的“发生在之前”关系到什么程度。显然,编译器可以保证在同一个代码块内,但是封闭 block 和对等 block 又如何——这就是我的问题所在。 @Stephen C 说,volatile 保证发生在整个方法体内的行为之前,即使在封闭 block 中也是如此,但我找不到任何证实。他是对的吗,有什么地方可以确认吗?
让我再举一个关于范围界定的具体例子来澄清事情:
setVolatile() {
l = 5;
}
callTheSet() {
i = 6;
setVolatile();
}
在这种情况下,编译器会禁止对 i
write 重新排序吗?或者也许编译器不能/没有被编程为在易变的情况下跟踪其他方法中发生的事情,并且 i
写入可以重新排序以发生在 setVolatile()
之前?或者编译器可能根本不重新排序方法调用?
我的意思是在某个地方一定有一点,编译器将无法跟踪某些代码是否应该在某些 volatile 字段写入之前发生。否则,一个 volatile 字段的写入/读取可能会影响一半程序的顺序,如果不是更多的话。这种情况很少见,但有可能。
此外,看看这句话
Under the new memory model, it is still true that volatile variables cannot be reordered with each other. The difference is that it is now no longer so easy to reorder normal field accesses around them.
“在他们周围”。这句话暗示,volatile 字段可以在一定范围内阻止重新排序。
最佳答案
Obviously, write to l will prevent write to k from reordering, but will it prevent reordering of writes to i and j?
重新排序的意思并不完全清楚;看我上面的评论。
但是,在 Java 5+ 内存模型中,我们可以说对 i
和 j
的写入发生在对 l
的写入之前> 在读取 l
后将对另一个线程可见 ... 假设在写入 l 后没有写入
。i
和 j
这确实具有限制写入 i
和 j
的指令的任何重新排序的效果。具体来说,在写入 l
之后,它们不能移动到内存写入屏障之后,因为这可能导致它们对第二个线程不可见。
But what is the scope of this prohibition?
没有禁令本身。
您需要了解指令、重新排序和内存屏障只是实现 Java 内存模型的特定方式的细节。该模型实际上根据保证在任何“格式良好的执行”中可见的内容来定义。
As I understand volatile prevents reordering inside the block where it is used, am I right?
实际上,没有。这些 block 不在考虑之列。重要的是方法中语句的(程序源代码)顺序。
@Stephen C said, that volatile guarantees happen before behavior inside the whole method's body, even in the enclosing block, but I can not find any confirmation to that.
确认为JLS 17.4.3 .它声明如下:
Among all the inter-thread actions performed by each thread t, the program order of t is a total order that reflects the order in which these actions would be performed according to the intra-thread semantics of t.
A set of actions is sequentially consistent if all actions occur in a total order (the execution order) that is consistent with program order, and furthermore, each read r of a variable v sees the value written by the write w to v such that:
w comes before r in the execution order, and
there is no other write w' such that w comes before w' and w' comes before r in the execution order.
Sequential consistency is a very strong guarantee that is made about visibility and ordering in an execution of a program. Within a sequentially consistent execution, there is a total order over all individual actions (such as reads and writes) which is consistent with the order of the program, and each individual action is atomic and is immediately visible to every thread.
If a program has no data races, then all executions of the program will appear to be sequentially consistent.
请注意,此定义中没有提及 block 或范围。
关于Java volatile 重排序预防作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51647337/
我已经能够通过 bean:write 消息中的 filter="true"组合以及我正在使用的标签库中使用 StringEscapeUtils.escapeHtml4(string) 来防止 stru
有什么办法可以防止 magento 中的 xss 攻击?在我的本地主机中,我只是想检查如何防止 xss 攻击,例如我在用户在 magento 中注册时插入脚本,在名称字段中插入整个脚本时我感到震惊我已
我是 Laravel 的新手,我正在学习它。 我们在 laravel 中做了什么来防止 sql 注入(inject)??什么是依赖注入(inject)以及我们如何防止这种情况发生? 提前致谢 最佳答案
最近我被嵌入式系统上的堆溢出困扰了好几次。 这通常是由于写入堆上声明的数组超过了数组的大小,这必须超过其他堆声明的变量,然后导致非常未定义的行为 - 使得根本原因难以追踪。 是否有任何方法可以检测/防
有一个 Node.js 项目可以清理数据,还有一个用于 JavaScript 的 OWASP 库可以处理清理以防止 XSS。 我一直在对这些库进行基准测试,它们非常密集,可能有点矫枉过正,我的应用程序
我的情况有点不同,我正在使用 CKEditor 进行编辑和显示,提交的字符串只会在 CKEditor 中显示,不会显示在其他任何地方。 我试过这个 XSS: alert("XSS")"> 我直接从后端
这个问题已经有答案了: How can I prevent a click on a '#' link from jumping to top of page? (25 个回答) 已关闭 5 年前。
通过对代码进行审查,可以轻松地识别和消除无效代码,但是,对于图像-未使用的图像仍会进入我们的版本控制。是否有任何组织图形内容的干净方法,以便网页和图像文件之间存在直接关联? 在我们当前的项目中,我们使
在我们的项目中,为了防止 XSS,我们添加了过滤器 (HttpServletFilter),它可以简单地转义 Json 中所有出现的“”(包装用户输入),例如: json = json.replace
我使用 Java JNI Gdal 。有一些服务器端应用程序构建在 JNI 绑定(bind)之上。如果 JNI 部分出现错误,整个 JVM 堆栈就会崩溃。 测试 C/C++ 库不包含会导致 JVM s
黑客通过 SQL 注入(inject)并运行虚假查询来攻击我的网站,如何防止他们需要帮助。 是否有任何防火墙可以防止黑客攻击等,或者我应该使用mysql_real_escape_string($_RE
我目前正在处理一个遗留的 ASP 项目,其中安全性现在已成为一个大问题。它不仅是不安全的加密方法(md5),而且我担心 SQL 注入(inject)问题。我还不太擅长注入(inject),而且我只尝试
我有兴趣了解如何在我的 ColdFusion 9 应用程序中防止跨站点请求伪造 (CSRF)。我在网上找到了一些教程,但似乎没有一个是全面的。我发现的最好的是:http://www.mollerus.
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我们的应用程序面临 XSS 攻击的问题。我们通过对 GET 请求使用普通过滤器来防止这种情况发生。 我们正在使用 RESTEasy REST Web 服务调用我们的应用程序。我们的过滤器不会过滤表单
我已经读过带有 2 个参数的 open() 命令容易被注入(inject),而带有 3 个参数的 open() 命令不能被注入(inject)。 说我有一个目录,我的所有文件都有一个公共(public
你好, 我正在尝试使用 CKEditor(一个 javascript WYSIWYG 编辑器)防止输入字段中的 XSS 和不正确的 html。 我应该如何在服务器端过滤这些数据?我比较的两个选项是 P
我刚刚了解了 CSRF 预防的细节。在我们的应用程序中,所有“写入”请求都是使用 XHR 完成的。整个页面实际上并没有提交任何一个表单,一切都是通过 XHR 完成的。 对于这种情况,维基百科建议 Co
据我所知,CSRF 预防似乎侧重于 (1) 使 GET 请求无副作用,以及 (2) 仅使用带有 CSRF token 的 POST 请求来更改状态。但在我看来,这假设攻击者的唯一目标可能是恶意更新受害
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 已关闭 9 年前。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来
我是一名优秀的程序员,十分优秀!