长变量的原子增量? [英] atomic increment of long variable?

查看:91
本文介绍了长变量的原子增量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果长变量声明为: -

if a long variable is declared as :-

private volatile long counter = 0;

现在如果我使用pre-increment运算符增加它,那么操作是原子的吗?

now if i increment it using pre-increment operator, then would the operation be atomic ?

如果是, code> java.util.concurrent.atomic.AtomicLong 对象的增量

if yes, then will it be more efficient than java.util.concurrent.atomic.AtomicLong object's increment ??

推荐答案

volatile 关键字只解决公开范围问题。您必须使用 AtomicLong 同步方法/区块的原子性(并发编程中的致命性)。

volatile keyword only solves visibility problem. You have to use AtomicLong or synchronized method/block for atomicity (Atomicity in concurrent programming).

今天发布的另一篇文章:展示何时需要挥发性物质

One more article that came out today: Demonstrating when volatile is required

这篇关于长变量的原子增量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆