"is atomicinteger.incrementandget thread safe?" Code Answer


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.

By Harrison on September 11 2022

Answers related to “is atomicinteger.incrementandget thread safe?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.