Atomicinteger Variable Learning

Source: Internet
Author: User

This is a example of how to use the AtomicInteger class of Java. Thejava.util.concurrent.atomic package provides very useful classes the support Lock-free and Thread-safe programming on Single variables. Among them, the AtomicInteger class is a wrapper class for a int value of allows it to be updated atomically. The class provides useful methods, some of which would be shown in the code snippet below.

The most common use of the was to AtomicInteger handle a counter that's accessed by different threads simultaneously. In order to see how the this works, we'll create and run both, each one Threads of which would access and update an AtomicInteger VARIAB Le, using its API methods. The basic methods used in the example is described in short:

Understanding: The most common usage of atomicinteger is as a counter between multiple threads.

  • incrementAndGet()with API method, the value was incremented and its new value is returned.
  • getAndIncrement()with API method, the value of is incremented, but it previous value is returned.
  • addAndGet(int delta)with API method, the was delta added to the value and the new value are returned, whereas there is also a getAndAdd(int delta) method That's adds delta the value, but returns the previous value.
  • With compareAndSet(int expect, int update) API method, the value was compared to expect the Param, and if they was equal, then the value was set update to the Param and is true returned.
  • You can get the the, int long , float or value of the double AtomicInteger variable, using intValue() , longValue() , and floatValue() doubleValue() methods re Spectivelly.
 PackageCom.javacodegeeks.snippets.core;ImportJava.util.concurrent.atomic.AtomicInteger; Public classAtomicintegerexample {Private StaticAtomicinteger at =NewAtomicinteger (0); Static classMyrunnableImplementsRunnable {Private intMyCounter; Private intMyprevcounter; Private intmycounterplusfive; Private BooleanIsnine;  Public voidrun () {MyCounter=At.incrementandget (); System.out.println ("Thread" + thread.currentthread (). GetId () + "/Counter:" +myCounter); Myprevcounter=at.getandincrement (); System.out.println ("Thread" + thread.currentthread (). GetId () + "/Previous:" +myprevcounter); Mycounterplusfive= At.addandget (5); System.out.println ("Thread" + thread.currentthread (). GetId () + "/Plus Five:" +mycounterplusfive); Isnine= At.compareandset (9, 3); if(isnine) {System.out.println ("Thread" +Thread.CurrentThread (). GetId ()+ "/Value is equal to 9, so it is updated to" +At.intvalue ()); }        }    }     Public Static voidMain (string[] args) {Thread T1=NewThread (Newmyrunnable ()); Thread T2=NewThread (Newmyrunnable ());        T1.start ();    T2.start (); }}

If You run the example, you'll see that both threads can update the AtomicInteger variable atomically.

Thread 9  /counter:1  /counter:29/previous:29/plus five:9 9/val UE is equal to 9, so it is updated to 310/previous:310/plus five:8

Atomicinteger Variable Learning

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.