Yes, the incrementAndGet
is thread safe.
incrementAndGet
and getAndIncrement
are the same as ++i
vs i++
. I.e.
int i = 0;
i++ // <- expression returns 0, i is now 1
++i // <- expression return 2, i is now 2
Ditto with decrement
.
Your code, however, is not threadsafe as you call get
twice across the ||
in the if
. So the value could be changed between value.get(i)
and value.get(j)
, whatever value
is.