I cannot decrement a common Long-value within a stream (outer declared value used within streams must be final), so I must use AtomicLong
within java streams:
var users = Stream<User> getUsers();
var dec = new AtomicLong(10);
long dec = 10;
// users is a
users.forEach(u->{
// does not work within streams, so I have to use AtomicLong
dec--;
// this works
dec.decrementAndGet();
// which one should I use, if I only want to get the raw value?
long actualValue = dec.getPlain();
long actualValue = dec.get(); // the same as dec.getOpaque();
});
I cannot see any differences between dec.getPlain()
and dec.get()
. I dont understand the
with memory semantics of reading
described in the API. Where lies the differences in those methods?
Which should I use if I only have one thread which reads the actualValue
.