Java原子整数和C# Interlocked.Increment方法之间的区别
我只是想知道,在线程环境中 Java 和 C# 中增加静态变量的方式是否有区别?
在 Java 中,您使用原子 int:s 来进行此操作,在 C# 中,您使用 Interlocked.Incement(ref yourVar)
我所说的并不是您编写的代码,而是它实际上如何锁定内存并执行实际增量。
Im just wondering, is there a difference between how you increment a static variable in Java and C# in an threaded enviroment?
In Java you use atomic int:s to make this operation and in C# you use Interlocked.Incement(ref yourVar)
I by this dont mean the code you write but how it is actually locks the memory and does the actual increment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
互锁操作不会锁定内存,而是根据操作向指令发出 LOCK 前缀。这会导致处理器断言总线锁定,因此只执行一次指令。
您可以进一步查看以下文章
链接不再有效,这里是存档版本
https://web.archive.org/web/20140325112655/http://lists.canonical.org/pipermail/kragen-tol/1999-August/000457.html
Interlocked operation doest not lock memory, it rather emits LOCK prefix to the instruction depending on the operation. That cause processor to assert bus lock so only instruction is executed once.
You can further look at the following article
Since the link is no longer working here is the archived version
https://web.archive.org/web/20140325112655/http://lists.canonical.org/pipermail/kragen-tol/1999-August/000457.html
就 Java 而言,“它如何工作”取决于执行平台的指令集。我(今天早些时候在等待无休止的操作系统升级完成时)读到,x86 上的 AtomicXxx 类是使用比较和交换 (CAS) 指令实现的。
In the case of Java - the "how it works" depends on the instruction set of the execution platform. I was reading (earlier today while waiting for an interminable OS upgrade to finish) that on x86 AtomicXxx classes are implemented using a Compare and Swap (CAS) instruction.