Java's multithreaded programming model 5--starting from Atomicinteger

Source: Internet
Author: User

Atomicinteger, a class that provides an integer for atomic manipulation. In the Java language, ++i and i++ operations are not thread-safe and, when used, will inevitably use the Synchronized keyword. The Atomicinteger is a thread-safe plus-minus operation interface.

Look at the interfaces provided by Atomicinteger.

Gets the current value

Public final int Get ()

Take the current value and set the new value

Public final int Getandset (int newvalue)

Gets the current value, and self-increment

Public final int getandincrement ()

To get the current value, and to subtract from the

Public final int getanddecrement ()

Gets the current value, plus the expected value

Public final int getandadd (int delta)

... ...

The CAs we mentioned in the previous section are mainly these two methods

Public final Boolean compareandset (int expect, int update) {
Return Unsafe.compareandswapint (this, valueoffset, expect, update);
}

Public final Boolean weakcompareandset (int expect, int update) {
Return Unsafe.compareandswapint (this, valueoffset, expect, update);
}

The two methods are different names, but the same thing is done, and may show a difference in the subsequent Java version.

Detailed review will find that both interfaces are called an unsafe class to operate, this is a local method implemented by JNI, the details are not considered.

Here is a comparison test, we write a synchronized method and a Atomicinteger method to test, intuitively feel the difference in performance

[Java] view plaincopy

  1. Package  zl.study.concurrency;

  2. Import  Java.util.concurrent.atomic.AtomicInteger;

  3. public class   atomicintegercomparetest {  

  4. Private   int value;

  5.      public   Atomicintegercomparetest ( int

  6. This  . value = value;

  7. }

  8.      public synchronized   int  increase () {   

  9. return  value++;

  10. }

  11.      public static void

  12. Long  start = System.currenttimemillis ();

  13. Atomicintegercomparetest test = new atomicintegercomparetest (0);

  14. for( inti=0;i<1000000; i++) {  

  15. Test.increase ();

  16. }

  17. Long  end = System.currenttimemillis ();

  18. System.out.println ("Time elapse:"+ (End-start));

  19. Long  Start1 = System.currenttimemillis ();

  20. Atomicinteger atomic = new atomicinteger (0);

  21. for( inti=0;i<1000000; i++) {  

  22. Atomic.incrementandget ();

  23. }

  24. Long  end1 = System.currenttimemillis ();

  25. System.out.println ("Time elapse:"+ (END1-START1));

  26. }

  27. }

Results

Time elapse:31
Time Elapse:16
It is not difficult to see that the local CAS performance by JNI is far beyond the synchronized keyword

Reference

http://stackoverflow.com/questions/2443239/ Java-atomicinteger-what-are-the-differences-between-compareandset-and-weakcompar

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.


Multithreaded programming model for Java 5--starting from 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.