What are the atomic operations of Java?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.