问题标签 [memory-visibility]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
7 回答
101018 浏览

java - 线程之间是否共享静态变量?

我在高级 Java 线程课程中的老师说了一些我不确定的话。

他表示,以下代码不一定会更新ready变量。据他介绍,两个线程不一定共享静态变量,特别是在每个线程(主线程与ReaderThread)都在自己的处理器上运行并且因此不共享相同的寄存器/缓存/等和一个 CPU不会更新另一个。

本质上,他说有可能ready在主线程中更新,但不在 中ReaderThread,因此ReaderThread将无限循环。

他还声称该程序可以打印042. 我了解如何42打印,但不是0。他提到当number变量设置为默认值时会出现这种情况。

我想也许不能保证在线程之间更新静态变量,但这让我觉得 Java 很奇怪。使readyvolatile 可以解决这个问题吗?

他展示了这段代码:

0 投票
2 回答
335 浏览

java - 使用构建器/工厂模式确保内存可见性

以下类:

可以在构建器模式中使用,并且在构建后,它实际上是不可变的,因为每个属性只能设置一次。那是:

但是,Pizza它不是线程安全的:不能保证线程 B 看到由线程 A 设置的成分。有一些方法可以修复它:

  • 做会员final。但是你不能使用构建器模式。
  • 同步访问成员。但这似乎是浪费,因为它们只写过一次。
  • 制作它们volatile。感觉很浪费,就像同步一样。
  • 使用AtomicReference.
  • ETC。?

我的问题是,告诉 JVM 在调用某个方法后类成员不会更改的最佳方法是什么?我应该只同步对它的访问,并相信 JVM 会优化锁吗?只是感觉很浪费,因为我知道成员应该表现得像它final设置后一样。没有更好的解决方案吗?

0 投票
4 回答
710 浏览

java - jvm重新排序/可见性效果测试

在写一些java文章时,我试图在多线程环境中不同步的对象构造的情况下重现重新排序。当构造一个重对象时没有同步/易失性/finals 并且其他线程在构造函数调用后立即访问它的情况。这是我尝试的代码:

- 但到目前为止没有效果,我已经在不同的 2,4 和 8 核 Intel/AMD PC 上尝试了 Windows,运行了几个小时的测试 - 没有重新排序效果 - System.err.printf("watcher %s see .. .") - 未调用,静态 sharedArray[randomIndex] 引用始终包含完全构造的值。

怎么了?如何重现这个?

0 投票
4 回答
394 浏览

java - 在构造函数调用后,其他线程在哪些架构/操作系统中可以看到默认的非最终字段值?

如果非最终字段的对象初始化不足(JLS 17.5 Final Field SemanticsFinalFieldExample类示例),我正在尝试重现内存可见性问题。它声明“但是,fy 不是最终的;因此不能保证 reader() 方法看到它的值 4”

我试过这段代码:

之前我的类似主题一样- 我已经尝试在不同的 PC(从 2 到 8 个核心)上使用 Windows,甚至在我们的服务器端 Solaris 32 核心盒上 - 我无法重现它:fx 和 fy - 总是正确的-初始化。

对于 Intel/x86/x64 架构,我得到了答案——它们有很多默认的内存保证,可以防止这种构造函数逻辑重新排序。Solaris/sparc 似乎也是如此?

那么在哪些架构/操作系统中可以重现这种重新排序?

0 投票
5 回答
2628 浏览

java - java中整数线程的非同步读取是安全的吗?

我在一些 OSS 单元测试中经常看到这段代码,但它是线程安全的吗?while 循环是否保证看到 invoc 的正确值?

如果不; nerd 指出谁也知道这可能会在哪个 CPU 架构上失败。

0 投票
2 回答
1665 浏览

java - 易变变量和其他变量

以下来自经典Concurency in Practice

当线程 A 写入 volatile 变量并且随后线程 B 读取相同的变量时,在写入 volatile 变量之前对 A 可见 的所有变量的值在读取 volatile 变量后对 B 可见。

我不确定我是否能真正理解这句话。例如,在这种情况下所有变量的含义是什么?这是否意味着 usingvolatile对非易失性变量的使用也有副作用?
在我看来,这句话有一些我无法理解的微妙含义。
有什么帮助吗?

0 投票
2 回答
560 浏览

java - 为什么这个简单的线程程序会卡住?

看一下这个简单的 Java 程序:

在一台机器上,它打印“再见”并立即退出,而在另一台机器上,它不打印任何东西并永远坐在那里。为什么?

0 投票
5 回答
50131 浏览

java - Why does this Java program terminate despite that apparently it shouldn't (and didn't)?

A sensitive operation in my lab today went completely wrong. An actuator on an electron microscope went over its boundary, and after a chain of events I lost $12 million of equipment. I've narrowed down over 40K lines in the faulty module to this:

Some samples of the output I'm getting:

Since there isn't any floating point arithmetic here, and we all know signed integers behave well on overflow in Java, I'd think there's nothing wrong with this code. However, despite the output indicating that the program didn't reach the exit condition, it reached the exit condition (it was both reached and not reached?). Why?


I've noticed this doesn't happen in some environments. I'm on OpenJDK 6 on 64-bit Linux.

0 投票
3 回答
1026 浏览

java - Uninitialized object leaked to another thread despite no code explicitly leaking it?

Let's see this simple Java program:

Main thread

The main thread creates an instance of B with x set to 1, then writes that instance to the static field A.b. It repeats this action forever.

Polling thread

The spawned thread polls until it finds that A.b.x is not 1.

?!?

Half the time it goes in an infinite loop as expected, but half the time I get this output:

Why is the polling thread able to see a B that has x not set to 1?


x%2 instead of just x is here simply because the issue is reproducible with it.


I'm running openjdk 6 on linux x64.

0 投票
1 回答
166 浏览

java - 将数组变量的值分配给自身?

我有这段简单的代码。

它输出我所期望的:

奇怪的是a=ain zzz()。当我拿出它时,它似乎没有任何改变。为什么会在那里?