首页 \ 问答 \ 深入理解Java中的volatile(Deep understanding of volatile in Java)

深入理解Java中的volatile(Deep understanding of volatile in Java)

Java允许输出1, 0吗? 我已经对它进行了非常密集的测试,但我无法获得该输出。 我只得到1, 10, 00, 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 only 1, 1 or 0, 0 or 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);
    }
}

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) before store(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
更新时间:2023-05-24 08:05

最满意答案

像这样的东西:

onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"

Something like this:

onclick="setTimeout(function() { updateLayer('text', 'ff', 'ok'); } ),1250);"

相关问答

更多
  • 避免将字符串传递给setTimeout 。 在可能的情况下,使用匿名函数: window.setTimeout(function () { PageMethods.UpdateForcedDisposition( forcedDisposition, a.value, SucceededCallback, FailedCallback ); }, 1000); 带有字符串的setTimeout在全局范围内执行。 如果您试 ...
  • 这可能与您使用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,并且 ...
  • 当您更改位置时,窗口对象及其所有相关事物(包括计时器)都将被丢弃,并创建一个新对象。 您不能安排代码在旧文档中运行新文档(甚至不能从浏览器控制台运行)。 在导航到新页面之后,您不得不粘贴并执行代码,这意味着您无法从代码中导航到代码。 您可以查看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 ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)