atomicinteger
The packagejava.concurrent.atomic
contains many useful classes to perform atomic operations. An operation is atomic when you can safely perform the operation in parallel on multiple threads without using the synchronized
keyword or locks.
By using
AtomicInteger
as a replacement for
Integer
we're able to increment the number concurrently in a thread-safe manor without synchronizing the access to the variable. The method
incrementAndGet()
is an atomic operation so we can safely call this method from multiple threads.
Last updated
Was this helpful?