问题描述
当返回值不重要时, AtomicInteger.getAndIncrement()
和 AtomicInteger.incrementAndGet()
方法,如果忽略返回值?
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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!