Atomic operations in Java include the following:
1) Assignment operation of basic types except long and double
2) All assignment operations that reference reference
3) java.concurrent.atomic.* All operations of all classes in the package
Count++ is not an atomic operation, it is a combination of 3 atomic operations
1. Read the Count value in main memory and assign the value to a local member variable TMP
2.tmp+1
3. Assign TMP to count
It may occur when thread 1 runs to step 2nd, the TMP value is 1, when the CPU schedule switches to thread 2 execution completes, the count value is 1, switch to thread 1, proceed to step 3rd, Count is assigned to 1------------The result is that two threads have finished executing, The value of Count is only added 1;
It is also important to note that if you use Atomicinteger.set (Atomicinteger.get () + 1), you will have concurrency problems as described above, and you will need to use atomicinteger.getandincrement () to avoid concurrency problems
What are the atomic operations of Java?