gpt4 book ai didi

java - 如果多个对象访问新的对象实例那么不需要同步?

转载 作者:行者123 更新时间:2023-11-29 07:40:46 25 4
gpt4 key购买 nike

https://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html

Counter is designed so that each invocation of increment will add 1 to c, and each invocation of decrement will subtract 1 from c. However, if a Counter object is referenced from multiple threads, interference between threads may prevent this from happening as expected.

如果多个线程引用同一个 Counter 对象实例,这是否成立?如果多个线程各自访问一个新的唯一(对于该线程)计数器实例,则不需要同步?

最佳答案

我要引用你的 link :

[...] However, if a Counter object is referenced from multiple threads, interference between threads may prevent this from happening as expected.

Interference happens when two operations, running in different threads, but acting on the same data, interleave. This means that the two operations consist of multiple steps, and the sequences of steps overlap.

如果您的每个线程都有自己的 Counter 实例,那么它们不会作用于相同的数据。因此,没有干扰,不需要同步。

关于java - 如果多个对象访问新的对象实例那么不需要同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30463383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com