- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
this question中有这么一句话:
All caches are coherent. That is, you will never had two different values for the same memory location. This coherency is maintained by a version of the MESI protocol
我的问题是为什么下面代码块中的iv.stop
总是false
,看来缓存一致性没有生效。顺便说一句,我电脑的 CPU 是 i7-4700HQ。
我绝对知道共享变量stop
的读取操作和写入操作之间不存在happens-before关系,这是Java中的数据竞争。我只是想知道为什么缓存一致性不起作用。由于线程 t2 更改了其运行核心中 stop
的缓存,因此根据缓存一致性,线程 t1 正在运行的核心缓存似乎应该看到此更改。
public class InfiniteLoop {
boolean stop = false;
Boolean another = null;
public static void main(String[] args) {
final InfiniteLoop iv = new InfiniteLoop();
Thread t1 = new Thread(() -> {
System.out.println("t1 iv address-->"+iv); //t1 iv address-->com.nestvision.thread.InfiniteLoop@48b96cb8
while (!iv.stop) {
//Not System.out.println(iv.stop) here to avoid
//a lock action of PrintStream object.
iv.another = iv.stop;
}
System.out.println("done");
});
Thread t2 = new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2 iv address-->"+iv);//t2 iv address-->com.nestvision.thread.InfiniteLoop@48b96cb8
iv.stop = true;
System.out.println("t2 end");
});
t2.start();
t1.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(iv.another); //this will print 'false'
}
}
最佳答案
首先,最后的打印可以在线程开始之前打印,并且肯定在线程结束之前打印。您可能想要使用一些 java 并发类来阻塞主线程,直到其他线程完成尽管这可能会无限循环并永远等待,直到您执行下面的修复。
另一个问题是您的 stop
变量不是 volatile
,因此不同的线程可能看不到该值。
这个其他答案包含有关跨处理器的 boolean 值和 L1/L2 缓存的信息 How is memory inconsistency different from thread interleaving?
相关引用:
Thread A reads the boolean value: it will necessarily read false (see the previous bullet point). When this read happens, it might happen that some memory pages, including the one containing that boolean, will be cached into Core 1's L1 cache, or L2 cache -- any cache local to that specific core.
Thread A negates and stores the boolean value: it will store true now. But the question is: where? Until a happens-before occur, Thread A is free to store this new value only on the local cache to the Core running the thread. Therefore, it is possible that the value will be updated on Core 1's L1/L2 cache, but will remain unchanged in the processor's L3 cache or in RAM.
After some time (according to your wall clock), Thread B reads the boolean value: if Thread A didn't flush the changes into L3 or RAM, it's entirely possible that Thread B will read false. If Thread A, on the other hand, had flushed the changes, it is possible that Thread B will read true (but still not guaranteed -- Thread B might have received a copy of Thread M's view of the memory and, due to the lack of happens-before, it won't go to the RAM again and will still see the original value).
The only way to guarantee anything is to have an explicit happens-before: it would force Thread A to flush its memory, and would force Thread B to read not from a local cache, but truly read it from the "authoritative" source.
Without a happens-before, as you can see form the example above, anything can happen, no matter how much time (from your perspective) elapses between events in different threads.
...
If that boolean variable is marked as volatile...then, only then, Thread B is guaranteed to see true (ie, otherwise there are no guarantees at all).
The reason is that volatile helps establish happens-before relationships. It goes as follows: a write to a volatile variable happens-before any subsequent read to the same variable.
关于java - 缓存一致性在无限循环演示中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765485/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!