Skip to content

Instantly share code, notes, and snippets.

@softberries
Created June 24, 2020 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softberries/8759c2dba0b8592c7e81e574b599c651 to your computer and use it in GitHub Desktop.
Save softberries/8759c2dba0b8592c7e81e574b599c651 to your computer and use it in GitHub Desktop.
/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final int incrementAndGet() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment