首页 \ 问答 \ 为什么Volatile变量不用于Atomicity(Why Volatile variable isn't used for Atomicity)

为什么Volatile变量不用于Atomicity(Why Volatile variable isn't used for Atomicity)

来自Javadocs

使用volatile变量可以降低内存一致性错误的风险,因为任何对volatile变量的写入都会与随后的同一个变量的读取之间建立一个happen-before关系。 这意味着对volatile变量的更改始终对其他线程可见。

对任何其他线程总是可见时,对volatile变量所做的更改,那么为什么在多线程写入该变量的情况下会使用volatile变量。 为什么volatile仅用于一个线程正在写入或正在读取的情况,而另一个线程正在读取该变量?

如果其他线程始终可以看到更改,那么假设线程B想要写入该变量,它将看到新值(由线程A更新)并更新它。 当线程A再次想要写入时,它会再次看到线程B的更新值并写入它。问题在哪里?

总之,我无法理解这一点。

如果两个线程都读取和写入共享变量,那么使用volatile关键字是不够的。 在这种情况下,您需要使用同步来确保变量的读写是原子的。


From Javadocs

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.

When changes made to a volatile variable are always visible to any other thread, then why volatile variable cant be used in case of multiple threads writing to that variable. Why is volatile only used for cases when one thread is writing or reading to that while the other thread is only reading the variable?

If changes are always visible to other threads, then suppose if thread B wants to write to that variable, it will see the new value(updated by thread A) and update it. And when the thread A again wants to write, it will again see the updated value by thread B and write to it.Where is the problem in that?

In short, i am not able to understand this.

if two threads are both reading and writing to a shared variable, then using the volatile keyword for that is not enough. You need to use synchronization in that case to guarantee that the reading and writing of the variable is atomic.


原文:https://stackoverflow.com/questions/30430128
更新时间:2023-06-27 22:06

最满意答案

HTML中的空格编码仅仅是一个空格。 nbsp可能看起来像一个空间,但具有不同的语义,“不中断”意味着换行符被抑制。

解决方案:每当我发现功能缺失或出现意外行为时(例如,asp:Label和HyperLink不进行HTML编码),我会写我自己的实用程序类,就像我说的那样;)

public class MyHtml
{
    public static string Encode(string s)
    {
        if (string.IsNullOrEmpty(s) || s.Trim()=="")
            return "& nbsp;";
        return Html.Encode(s);
    }
}

A space encode in HTML is just a space. nbsp may look like a space, but has a different semantics, "non-breaking" meaning that line breaks are suppressed.

Solution: Whenever I find functionality lacking or with unexpected behavior (e.g. asp:Label and HyperLink don't HTML encode), I write my own utilities class which does as I say ;)

public class MyHtml
{
    public static string Encode(string s)
    {
        if (string.IsNullOrEmpty(s) || s.Trim()=="")
            return "& nbsp;";
        return Html.Encode(s);
    }
}

相关问答

更多
  • var str=" 地址:西安市 电话: 中的空格和换行用TextBox显示出来其中的 " ; response.write(server.htmlEncode(str))
  • 你不需要手动Html.Encode因为Razor以这种方式安全。 这意味着Razor HTML使用@ sing编码您写入输出的每个字符串。 所以你可以安全地写@value.Value 。 顺便说一下你所看到的效果是双重编码的情况,因为你首先使用Html.Encode对文本进行编码,然后当你用@Html.Encode(value.Value)写出来时,Razor第二次对已经编码的文本进行编码@Html.Encode(value.Value) 。 You don't need to manually Html. ...
  • 您需要检查字符串长度是否大于30,否则您指定的长度将从字符串的末尾开始...(我也将您的起始索引更改为0,假设您不想遗漏第一个字符) <%= Html.Encode(item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%> You need to check the strings length is greater than 30, otherwis ...
  • 你需要将它作为参数传递: @helper ShowDefinitionText(String text, HtmlHelper htmlHelper) { Definition:

    @htmlHelper.Encode("dogs")

    } 当从视图中调用帮助程序时,提供有效的实例: @ShowDefinitionText("some text", Html) 此外,如果你只是进行HTML编码,你可能不需要助手,因为@ Razo ...
  • 我发现了我的问题。 在visual studio中,我创建的每个文件都是UTF-8编码。 我需要做的是将编码从UTF-8更改为ANSI。 问题解决了。 I found the cuase of my problem. In visual studio each file i create is in UTF-8 encoding. What i need to do is change the encoding from UTF-8 to ANSI. Problem solved.
  • 这是编码两次 - 您是否在HtmlHelper调用中使用它? // this will displayÆØÅ as Html.TextBox encodes the // value passed to it so it's encoded twice in this line <%=Html.TextBox("sdfsdf", Html.Encode("ÆØÅ"))%>
    // this will display ÆØÅ <%= Html.Encode("ÆØÅ" ...
  • HTML中的空格编码仅仅是一个空格。 nbsp可能看起来像一个空间,但具有不同的语义,“不中断”意味着换行符被抑制。 解决方案:每当我发现功能缺失或出现意外行为时(例如,asp:Label和HyperLink不进行HTML编码),我会写我自己的实用程序类,就像我说的那样;) public class MyHtml { public static string Encode(string s) { if (string.IsNullOrEmpty(s) || s.Trim()= ...
  • 不要使用ViewData是我要说的第一件事。 将模型返回到包含标签的视图。 如果需要,创建复合模型。 然后,要编辑标签,请转到采用相同模型的视图,允许您输入新文本并将结果保存到xml,db,文本文件等等。 所以你可能有这样的模特; public class Labels { public List text{ get; set; } } 因此,在数据库层中,无论在何处,都使用标签文本项填充对象。 在您的编辑视图中,您应该执行以下操作; <% Html.PartialView("Edit ...
  • 问题在于你实际上并没有向响应流添加任何内容。 Html.Encode没有问题,但你需要做这样的事情: <%if (!ViewData.ContainsKey("DisplayQtyPrice")) { Response.Write(Html.Encode(entry.Amount)); }%> 编辑:这就是说,我认为你的第一个版本更好,除非你有尖括号的问题:) The problem is that you're not actually adding anything to the respon ...
  • 答案来自于我和我对该问题的评论中的讨论。 我不确定要回答什么,所以我决定回答我自己的问题。 我希望没关系。 似乎在设置属性的值(如文本框的“值”)时,.NET自动html对值进行编码,因此无需自己执行此操作。 虽然设置html控制内部HTML,但重要的是你要对值进行html编码。 谢谢Thejh,对不起,我无法对你写的任何内容进行投票。 编辑:我不能将此标记为另外2天的答案。 The answer came out of thejh's and my discussion in the comment to ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。