"Java" Thread-safe integer class Atomicinteger

Source: Internet
Author: User

First, Meet Atomicinteger

See this class when looking at the project code, find its function is simple, is an integer variable type, out of curiosity to see its class definition.

This class is located under Java.util.concurrent.atomic, and under concurrent, it is certain that the class is related to concurrency and atomicity.

Second, further understanding

The source code is very simple, combined with other people's blog, it is basic to understand that the Atomicinteger class is an integer class that provides atomic operations.

Ordinary integer classes, such as int and integer, are not thread-safe for operations such as ++i/i++, and are prone to dirty data in concurrent environments.

Atomicinteger uses the volatile keyword to decorate so that the class can meet thread safety.

    Private volatile intvalue; /*** Creates a new atomicinteger with the given initial value. *     * @paramInitialValue the initial value*/     PublicAtomicinteger (intInitialValue) {Value=InitialValue; }    /*** Creates a new Atomicinteger with initial value {@code0}.*/     PublicAtomicinteger () {}

In the source code also has the embodiment.

"Java" Thread-safe integer class Atomicinteger

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.