I have a program running multi-thread, all thread share and process on single AtomicLong variable. Each of them will call getAndAdd()
method first to retrieve value and process further.
If all threads running simultaneously, does calling above method cause one thread to waiting another thread to finish getting value?
The threads are not waiting (in the sense of
Object::wait
), they are looping until they succeed in getting and adding to theAtomicLong
.For reference, the code in JDK 8 looks like this:
Note that the method may be intrinsic (i.e. it is replaced at runtime by something more efficient, typically a single CPU instruction)