AtomicInteger.incrementAndGet()与AtomicInteger.getAndIncrement() [英] AtomicInteger.incrementAndGet() vs. AtomicInteger.getAndIncrement()

查看:137
本文介绍了AtomicInteger.incrementAndGet()与AtomicInteger.getAndIncrement()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当返回值不重要时,

When return value is not of interest, is there any (even irrelevant in practice) difference between AtomicInteger.getAndIncrement() and AtomicInteger.incrementAndGet() methods, when return value is ignored?

我正在考虑这样的差异,例如哪些差异更常见,以及哪些将使CPU高速缓存中的负载变得更少,或者进行其他任何事情,实际上可以帮助决定比投掷硬币更合理地使用哪一个.

I'm thinking of differences like which would be more idiomatic, as well as which would put less load in CPU caches getting synchronized, or anything else really, anything to help decide which one to use more rationally than tossing a coin.

推荐答案

由于未给出实际问题的答案,因此,我基于其他答案(感谢,支持)和Java约定得出我的个人看法:

Since no answer to the actual question has been given, here's my personal opinion based on the other answers (thanks, upvoted) and Java convention:

incrementAndGet()

更好,因为方法名称应以描述动作的动词开头,而此处的预期动作仅是递增.

is better, because method names should start with the verb describing the action, and intended action here is to increment only.

以动词开头是常见的Java约定,官方文档也对此进行了描述:

Starting with verb is the common Java convention, also described by official docs:

>方法应为动词,与首字母小写,每个内部单词的首字母大写."

这篇关于AtomicInteger.incrementAndGet()与AtomicInteger.getAndIncrement()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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