将基元写入堆栈或堆?

Writing of primitives to stack or heap?

我今天参加了工作面试。 有一个问题面试官问我一个线程读取另一个线程设置的整数值需要多少时间?微秒?毫秒甚至一秒?

他告诉我,长值甚至可以达到一秒,因为"the long value is written first to the stack but the reading threads read from the heap, so the value to be read should first be copied to the heap"而长值可能需要很多时间。

有人可以告诉我我是否理解正确并解释一下吗?

谢谢!

how much time will it take to a thread to read an integer value that another thread set? Micro seconds? Milliseconds or even a second?

取决于很多因素。如果问题是关于 volatile intlong 字段,那么现代 CPU 上的答案通常仍然是微秒。如果该字段没有其他线程的频繁访问,就性能而言它实际上可以非常接近正常读取。但是,如果变量竞争激烈,它可能会更加昂贵,具体取决于缓存失效和内存行锁定的成本。当然,如果您正在访问 synchronized 块内的字段,那么它取决于线程之间的锁争用以及块中的其他操作。

例如,在我的 4 核 Mac 上,运行 10 个线程都在递增 volatile int,它们可以在 ~190 毫秒内完成 100 万次 ++。如果我的数学是正确的,那是每 ~0.19 微秒。当然在任何方面都不科学,但它应该让你对规模有所了解。更改为 volatile long 并没有太大改变数字,但我在本机 64 位系统和 JVM 上是 运行。同样,具有大量缓存内存的大型应用程序内部的性能下降可能接近毫秒,但远不及秒。

为了比较,它可以在 ~1300 毫秒内完成 AtomicInteger 增量,在 ~1400 毫秒内完成 100 万 AtomicLong 增量。同样,这些数字是一个非常简单的测试程序中的近似值。

He told me that it can reach even a second in the case of a long value, because "the long value is written first to the stack but the reading threads read from the heap, so the value to be read should first be copied to the heap" and for long values it can take a lot of time.

这没有多大意义。 intlong 之间的唯一区别是 long 可以进行多次访问,具体取决于您的运行时体系结构。例如,如果您在 32 位架构上,可能需要多次访问才能读取和更新 64 位值。但是访问 long 需要几秒 只是 因为它是宽度的两倍的想法是无效的。

how much time will it take to a thread to read an integer value that another thread set?

正如其他人所提到的,如果这不是在谈论一个线程何时会看到另一个线程中对变量进行的非同步更新,那么答案肯定是秒,但这与变量的宽度无关与堆栈和堆之间的复制无关。