深入理解Java中的volatile(Deep understanding of volatile in Java)
Java允许输出
1, 0
吗? 我已经对它进行了非常密集的测试,但我无法获得该输出。 我只得到1, 1
或0, 0
或0, 1
。public class Main { private int x; private volatile int g; // Executed by thread #1 public void actor1(){ x = 1; g = 1; } // Executed by thread #2 public void actor2(){ put_on_screen_without_sync(g); put_on_screen_without_sync(x); } }
为什么?
在我眼中,有可能获得
1, 0
。 我的理由。g
是易失性的,因此会确保内存顺序。 所以,它看起来像:actor1: (1) store(x, 1) (2) store(g, 1) (3) memory_barrier // on x86
并且,我看到以下情况:在
store(x,1)
之前重新排序store(g, 1)
store(x,1)
(memory_barrier 在 (2)之后)。 现在,运行线程#2。 所以,g = 1, x = 0
。 现在,我们已经预期了产量。 我的推理有什么不对?Does Java allows output
1, 0
? I've tested it very intensively and I cannot get that output. I get only1, 1
or0, 0
or0, 1
.public class Main { private int x; private volatile int g; // Executed by thread #1 public void actor1(){ x = 1; g = 1; } // Executed by thread #2 public void actor2(){ put_on_screen_without_sync(g); put_on_screen_without_sync(x); } }
Why?
On my eye it is possible to get
1, 0
. My reasoning.g
is volatile so it causes that memory order will be ensured. So, it looks like:actor1: (1) store(x, 1) (2) store(g, 1) (3) memory_barrier // on x86
and, I see the following situation: reorder
store(g, 1)
beforestore(x,1)
(memory_barrier is after (2)). Now, run thread #2. So,g = 1, x = 0
. Now, we have expected output. What is incorrect in my reasoning?
原文:https://stackoverflow.com/questions/45133832
最满意答案
像这样的东西:
onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"
Something like this:
onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"
相关问答
更多-
Javascript setTimeout(Javascript setTimeout)[2023-08-26]
避免将字符串传递给setTimeout 。 在可能的情况下,使用匿名函数: window.setTimeout(function () { PageMethods.UpdateForcedDisposition( forcedDisposition, a.value, SucceededCallback, FailedCallback ); }, 1000); 带有字符串的setTimeout在全局范围内执行。 如果您试 ... -
表单提交上的setTimeout返回'not a function'错误(setTimeout on form submission returning 'not a function' error)[2023-11-04]
这可能与您使用setTimeout(其他人提出的问题)无关。 最可能的原因是你在表单中有一个名为submit(或者作为id的元素)的元素(可能是你与jQuery选择器匹配的元素),并且这已经替换了submit属性上的function一个HTMLElementNode 。 最简单的解决方案是重命名元素。 This likely has nothing to do with your use of setTimeout (which has issues raised by other people). The ... -
其他两个答案是正确的,但以防万一你的问题不是关于匿名函数,而是像这样 function onTimeout(){ console.log('foo'); }; setTimeout(onTimeout, 1000); // First form setTimeout(onTimeout(), 1000); // Second Form 区别在于,在第一种形式中,onTimeout函数在1秒后调用,这通常是期望的结果。 在第二种形式中,立即调用Timout,并且 ...
-
setTimeout点火太快(?)(setTimeout firing too soon(?))[2022-09-21]
当您更改位置时,窗口对象及其所有相关事物(包括计时器)都将被丢弃,并创建一个新对象。 您不能安排代码在旧文档中运行新文档(甚至不能从浏览器控制台运行)。 在导航到新页面之后,您不得不粘贴并执行代码,这意味着您无法从代码中导航到代码。 您可以查看TamperMonkey或GreaseMonkey等工具,并让您运行代码以响应与特定网址匹配的网页加载。 When you change the location, the window object and all of its associated things ... -
你需要“锚定”循环迭代器的值,否则它会不断递增,所有的间隔都会影响第29个,这个不存在。 for(var x=0; x<28; x++) { (function(x) { // code goes here })(x); } 但是,一页上有28个计时器是一个非常糟糕的主意。 考虑重写代码,以便只有一个间隔根据速度值计算新位置。 You need to "anchor" the value of your loop iterator, otherwise it keeps i ...
-
将方法传递给setTimeout() ,它将在全局范围内执行。 this将在执行时指向window 。 阅读更多这里 。 如果foo不是全局的,它将不会被找到,因为ReferenceError 。 var __nativeST__ = window.setTimeout, __nativeSI__ = window.setInterval; // just backed up the defaults. Now basically creating timeout and setInterval //fun ...
-
您在setTimeout中使用document.location.href。 它是一个重定向(GET方法),当然,POST数据不会发送。 尝试在setTimeout中使用 setTimeout(function() { $("#loginform").submit(); }, 2000); You use document.location.href in setTimeout. Its an redirect (GET method), a ...
-
像这样的东西: onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);" Something like this: onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"
-
人们可能没有意识到他们可以使用不带引号的形式。 可能尚未定义字符串中引用的名称。 引用的表单会延迟执行: setTimeout("myFunction(1, 'hello')", 100) 比以下更容易理解: setTimeout(function () { myFunction(1, 'hello') }, 100) 这不符合作者的要求: setTimeout(myFunction(1, 'hello'), 100) People may not realize they can use the u ...
-
将其更改为: setTimeout(numCount,1000); 使用()您将立即调用该函数。 上面是传递函数作为参考,它将在延迟时间后调用。 使用引号的另一种不太优选的方式是: setTimeout('numCount()',1000); // or setTimeout("numCount()",1000); 使用字符串参数..字符串将在计时器延迟结束时计算为脚本 如有疑问,请在MDN文档中查找 Change it to : setTimeout(numCount,1000); With the ...