为什么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
最满意答案
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); } }
相关问答
更多-
如何使用Html.Encode()将[2021-10-01]
var str=" 地址:西安市 电话: 中的空格和换行用TextBox显示出来其中的 " ; response.write(server.htmlEncode(str)) -
PartialView创建一个选择框:如何Html.Encode输出?(PartialView to create a select box: How to Html.Encode Output?)[2022-10-04]
你不需要手动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. ... -
html.encode上的子字符串(substring on html.encode)[2023-11-06]
您需要检查字符串长度是否大于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()= ...
-
ASP MVC ViewData从一个视图到另一个视图(Html.Encode())(ASP MVC ViewData from one view to another (Html.Encode()))[2023-08-17]
不要使用ViewData是我要说的第一件事。 将模型返回到包含标签的视图。 如果需要,创建复合模型。 然后,要编辑标签,请转到采用相同模型的视图,允许您输入新文本并将结果保存到xml,db,文本文件等等。 所以你可能有这样的模特; public class Labels { public Listtext{ 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 ...
-
如何以及何时使用Html编码(How and when to use Html encode)[2021-11-07]
答案来自于我和我对该问题的评论中的讨论。 我不确定要回答什么,所以我决定回答我自己的问题。 我希望没关系。 似乎在设置属性的值(如文本框的“值”)时,.NET自动html对值进行编码,因此无需自己执行此操作。 虽然设置html控制内部HTML,但重要的是你要对值进行html编码。 谢谢Thejh,对不起,我无法对你写的任何内容进行投票。 编辑:我不能将此标记为另外2天的答案。 The answer came out of thejh's and my discussion in the comment to ...