Follow by Email

Monday, 17 February 2014

AtomicInteger Java 7 vs Java 8

Atomic Integer is interesting class, it is used for building many lock free algorithm. Infact JDK locks are also build using ideas from Atomic datatypes.

As name suggest it is used for doing atomic increment/decremented, so you don't have to use locks, it will use processor level instruction to do so.
It is based on Compare-and-swap instruction.

Issue with CAS
CAS works on optimistic approach, it expects some failure, so it will retry operation, so in theory if there is no contention then it should work pretty fast.

There is another alternate way of doing same thing using Fetch-and-add.
Fetch-and-add is very different from CAS, it is not based on re-try loops.

Dave Dice compares CAS vs Fetch-and-add in atomic_fetch_and_add_vs blog, i can't explain better than this, so i will copy content from his blog


  1. CAS is "optimistic" and admits failure, whereas XADD does not. With XADD there's no explicit window of vulnerability to remote interference, and thus no need for a retry loop. Arguably, XADD has better progress properties, assuming the underlying XADD implementation doesn't have an implicit loop, but even in that case the window would be narrower than with Load;Φ;CAS.
  2. Lets say you were trying to increment a variable with the usual Load;INC;CAS loop. When the CAS starts failing with sufficient frequency you can find that the branch to exit the loop (normally taken under no or light contention) starts to predict toward the failure path. So when the CAS ultimately succeeds, you'll incur a branch mispredict, which can be quite painful on processors with deep pipelines and lots of out-of-order speculative machinery. Typically, this is in a piece of code where you don't want a long stall. There's no loop and no such issues with XADD.
Since fetch-and-add has predictable progress properties, so it is used for developing waiting free algorithms.
Unfortunately JDK 7 does not have support for fetch-and-add, one more reason why C++ people will be happy that C++ is great!


As we all know things do change & java community decided to added support for fetch-and-add in JDK8, one more good reason to migrate to JDK8.

In this blog i will compare performance of AtomicInteger from JDK7 & 8

Atomic Integer - JDK 7 vs JDK 8

In this test i increment counter 10 Million times with different number of threads. Thread numbers are increased to see how counter performs under contention.



X Axis - No of Threads
Y Axis - Ops/Second - Higher is better

JDK8 counter is winner in this case, best performance is when there is no contention , for JDK7 it is 80 MOPS but for JDK8 it close to 130 MOPS.
For single thread difference is not much , JDK8 is around 0.5 times faster but as contention increases performance JDK7 counter starts falling.

I will put another graph by removing 1 thread number, so that we can clearly see how these counter performs.


This gives better idea of how slow JDK7 atomic integer is, for 8 threads JDK8 counter is around 3.5X times faster.

Dive Into Code

JDK 8 - AtomicInteger
public final int getAndIncrement() {
        return unsafe.getAndAddInt(this, valueOffset, 1);
    }

JDK7 - AtomicInteger
 public final int getAndIncrement() {
        for (;;) {
            int current = get();
            int next = current + 1;
            if (compareAndSet(current, next))
                return current;
        }
    }

JDK8 is using new function(getAndAddInt) from unsafe to do the magic. Unsafe has become more useful!

Dive in Assembly
To just confirm that all performance again is coming from fetch-and-add i had look at assembly generated.

JDK 8 
0x0000000002cf49c7: mov    %rbp,0x10(%rsp)
  0x0000000002cf49cc: mov    $0x1,%eax
  0x0000000002cf49d1: lock xadd %eax,0xc(%rdx)  ;*invokevirtual getAndAddInt
                                                ; - java.util.concurrent.atomic.AtomicInteger::incrementAndGet@8 (line 186)


JDK 7

0x0000000002c207f5: lock cmpxchg %r8d,0xc(%rdx)
  0x0000000002c207fb: sete   %r11b
  0x0000000002c207ff: movzbl %r11b,%r11d        ;*invokevirtual compareAndSwapInt
                                                ; - java.util.concurrent.atomic.AtomicInteger::compareAndSet@9 (line 135)
                                                ; - java.util.concurrent.atomic.AtomicInteger::incrementAndGet@12 (line 206)

  
Conclusion
Introduction of fetch-and-add type of feature in java will make it more suitable for high performance computing, we will see more wait free algorithm in java

Code used for testing is available @ AtomicCounterTest
Just compile for jdk7/8 and execute it.

82 comments:

  1. All your blogs are interesting and informative. Attaching code snippets is a great add on.

    ReplyDelete
  2. Good to hear that you find is useful.
    I do add link to GitHub that has code, I avoid adding code snippets to keep blog small, but try to include some in future post.

    ReplyDelete
  3. It was very useful for me. Keep sharing such ideas in the future as well. Website Design Company Bangalore | Web Designing Bangalore

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. According to wiki https://en.wikipedia.org/wiki/Fetch-and-add a consensus number is equal to 2. My understanding of that fact is that only up to 2 threads can act on a shared AtomicInteger without some waiting/congestion.
    How would you explain significant drop in performance of fetch-and-add between 1 and 2 threads (and not between 2 and 3).
    Similarly, why fetch-and-add performance degradation while comparing 2 threads with 3 threads is of same magnitude as when comparing 3 and 4 threads?

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. I checked unsafe.getAndAddInt implementation in Java 8 and it is still using compareAndSwap:
    public final int getAndAddInt(Object var1, long var2, int var4) {
    int var5;
    do {
    var5 = this.getIntVolatile(var1, var2);
    } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));

    return var5;
    }

    I'm not sure where you're taking the information about the fetch and add

    ReplyDelete
    Replies
    1. I wrote short blog post to answer this question.
      http://ashkrit.blogspot.com/2017/07/java-intrinsic-magic.html

      Delete
  8. Super blog and very interesting information which I always wanted to search many article but you article is really fantastic.

    graphic designer in dubai

    ReplyDelete
  9. I’ve read several good stuff here. Definitely price bookmarking for revisiting. I wonder how a lot effort you put to create this sort of wonderful informative web site.
    Nadkaar - Dubai Website Design

    ReplyDelete
  10. Really informative and helpful blog. It gives a clear view of Java 7 and 8.
    SEO experts Dubai

    ReplyDelete
  11. good one, im waiting for more articlest like this. keep up the good work.
    SEO Dubai

    ReplyDelete
  12. This blog is really helpful regarding all educational knowledge I earned. It covered a great area of subject which can assist a lot of needy people. Everything mentioned here is clear and very useful.
    Content marketing montreal

    ReplyDelete
  13. Awesome post dude its so much informative for the followers and so much helpful also.I appreciate you for this great post.Thanks for sharing.Keep it up.

    Mobile App Development company Jordan | Digital Marketing Company Jordan | Blockchain Development Company in Jordan | Web Development Company in Jordan

    ReplyDelete
  14. Thanks for sharing the great post.
    SMS is a great way of doing marketing. Many business giants are using text messaging to increase their sales.

    ReplyDelete
  15. I found this post interesting and worth reading. Keep going and putting efforts into good things.
    Yasir Jamal - Web design Dubai

    ReplyDelete
  16. I have been reading your blogs and I really started liking it.
    SEO Dubai

    ReplyDelete
  17. This blog is useful as well as informative. Keep sharing such blogs I really like your posts.
    Greeting cards Dubai

    ReplyDelete
  18. Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this.
    Website Design
    SEO Company

    ReplyDelete
  19. This is really interesting post,Appreciate the effort in educating us. We do provide
    Web Design Company in Bangalore

    ReplyDelete
  20. hey i loved your site layout and especially the way you wrote everything. I must say that you keep posting this type of information so that we may see the latest news.
    Seo Expert In Pakistan

    ReplyDelete
  21. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us.pandaexpress.com/feedback

    ReplyDelete
  22. Hi,

    Thanks for sharing a very interesting article about AtomicInteger Java 7 vs Java 8. This is very useful information for online blog review readers. Keep it up such a nice posting like this.

    Regards,
    WondersMind,
    Best Website Design Company in Bangalore

    ReplyDelete
  23. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us. I must say that if you are interested in FMCdealer quick step by step login guide to ford employees then do visit FMC Dealer Direcrt Official Website fmcdealer

    ReplyDelete
  24. To find the best short term rentals in dubai i will suggest you to visit the MEX Holiday Homes Dubai office if you're looking to plan you vacations soon in Dubai.

    ReplyDelete

  25. ACME SURVEY
    : Step by Step Guide to take ACME customer satisfaction survey. Enjoy free coupons which can allow you to buy free things on next visit.
    Ready to win rewards? The latest & Updated Step by Step Guide for ACME survey online

    ReplyDelete
  26. Hey I loved the way you shared the valuable information with the community. I would say that please continue these efforts and we want to hear more from you.pandaexpress.com/feedbacktalktowendys

    ReplyDelete
  27. To find the best short term rentals in dubai i will suggest you to visit the MEX Short Term Rentals Dubai office if you're looking to plan you vacations soon in Dubai.

    ReplyDelete
  28. The most authentic post ever i read about this topic so far i must say you have cleared all the required info thank for sharing, keep up the good work and find best e-ticket, visa and travel insurance services at Malik Express.

    ReplyDelete
  29. Really like this post of yours about the latest activity you performed and shared with us thanks keep up the good work and don't forget to win $500 via TellTheBell survey by Taco Bell.

    ReplyDelete
  30. The most authentic post ever i read about the topic so far i must say you have cleared all the

    required info thank for sharing, keep up the good work and win $2 Gift Cards at moegottaknow survey.

    ReplyDelete
  31. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us.ncsecu login everydaysolutionsforu

    ReplyDelete
  32. Thanks for sharing your ideas. I was desperately waiting for such posts. I really appreciate your efforts and I will be waiting for your further write ups thanks once again.Seo Expert in Pakistan

    ReplyDelete
  33. I must say that the blog is great! I really like it an appreciate your efforts you put in it.
    SEO Services is an imperative marketing strategy that affects your business in a positive way by increasing the conversion rate and improving the sales.

    ReplyDelete

  34. Thank Your for sharing such a useful review.. If you want to visit Pakistan.. do Visit Hunza with Hunza Tour Packages we provide the best Tour services in Pakistan

    ReplyDelete
  35. Wonderful post! An opportunity to read a fantastic and imaginary blogs, I really like to read your blog, the points you have mentioned in this article are helpful for me, thank you so much for share this awesome blog post. We are the best award winning web development company in New Zealand. Please Visit Tandem NZ

    ReplyDelete
  36. It’s hard to come by experienced people about this subject, but you seem like you know what you’re talking about! Thanks
    getmyoffersguide.com

    ReplyDelete
  37. You ought to be a part of a contest for one of the finest sites on the internet. I will recommend this web site!
    https://www.creditscardsadviser.com/
    https://www.creditscardsbenefits.com/

    ReplyDelete
  38. Πολύ μεγάλο άρθρο. Σας ευχαριστώ. Μπορεί όλα τα καλά πράγματα να έρχονται σε σας. Αντίο

    Bồn ngâm massage chân

    Bồn ngâm chân

    Có nên dùng bồn ngâm chân

    Cách sử dụng bồn ngâm chân

    ReplyDelete
  39. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us. Win Prizes by taking surveys

    ReplyDelete
  40. Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
    SEO company in coimbatore
    Digital Marketing Company in Coimbatore
    SEO Services in coimbatore

    ReplyDelete
  41. As you grow, you will(nhà khung thép giá rẻ) realize that arguing right and wrong more than(so sánh gạch nung và gạch không nung) losing to others is sometimes not important anymore.(tam san be tong sieu nhe) Most importantly, just want peace.

    ReplyDelete
  42. Nice post!Everything about the future(giá nhà khung thép) is uncertain, but one thing is certain: God has set tomorrow for all of us(tấm bê tông siêu nhẹ). We must now trust him and in this regard, you must be(chi phí xây dựng nhà khung thép) very patient.

    ReplyDelete
  43. Hey Thanks for sharing this valuable information with us. I will come back to your site and keep sharing this information with us.
    Captions
    captions
    Captions
    captions

    ReplyDelete
  44. Amazing website. Great information provided. Learned a lot. Thank you
    https://platinumoffersonline.com/

    ReplyDelete
  45. Ever heard that Wendys is offering you the chance to take surveyforcustomers Customer Satisfaction Survey where you can win Free BOGO Sandwiches and much more at Wendys Restaurant.

    ReplyDelete
  46. The topic of this post is very interesting for me. I am very much impressed after reading this post and I must appreciate your effort in sharing this post with us here.
    Best affordable SEO Services

    ReplyDelete
  47. Lets state you were attempting to increase a variable with the typical Load;INC;CAS circle. At the point when the CAS begins falling flat with adequate recurrence you can find that the branch to leave the circle (ordinarily taken under no or light conflict) begins to anticipate toward the disappointment way. So when the CAS at sareen air last succeeds, you'll bring about a branch mispredict, which can be very difficult on processors with profound pipelines and bunches of out-of-request theoretical apparatus. Ordinarily, this is in a bit of code where you don't need a long slow down. There's no circle and no such issues with XADD.

    ReplyDelete
  48. What an amazing post you written really love the post please do tell me how to subscribe your blog as and one more thing visit https://kroger-feedback-survey.com/ and win free $5000 gift cards for Kroger grocery stores to shop for free.

    ReplyDelete
  49. ඔබේ කාර්යය සෑම විටම හොඳයි, වඩාත් රසවත් ලිපි තිබිය යුතුය.

    cần mua chó Poodle

    cách nuôi chó Poodle

    đặc điểm chó Poodle

    Nguồn gốc chó Poodle

    ReplyDelete
  50. Hi blogger, i must say you have hi quality articles here.
    Your website can go viral. You need initial traffic boost only.
    How to get it? Search for; make your content go viral Wrastain’s tools
    web application development company in India | ecommerce web development

    ReplyDelete
  51. នេះគឺជាផ្នែកមួយនៃអត្ថបទដ៏ត្រជាក់បំផុតនៃសតវត្សទី។ សូមអរគុណចំពោះការចែករំលែក។ សូមជូនពរអ្នកអោយមានសំណាងនិងជោគជ័យ!

    C.ty bán cửa lưới tại huyện Đông Anh

    Đại lý cửa lưới chống muỗi tại Quảng Ninh

    Siêu thị cửa lưới chống muỗi tại Linh Đàm

    Siêu thị bán cửa chống muỗi phường Phương Mai

    ReplyDelete
  52. Nice Blog, Thanks for sharing! Also check my website
    digital marketer in coimbatore

    ReplyDelete
  53. Bài viết bạn rất hay:



    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:



    lều xông hơi



    lều xông hơi sau sinh



    lều xông hơi giá bao nhiêu


    ReplyDelete
  54. Bài viết bạn rất hay:



    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:



    Giảo cổ lam



    giảo cổ lam giải độc gan



    giảo cổ lam giảm béo

    ReplyDelete
  55. Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks
    obat benjolan di labia mayora
    obat kista ovarium paling ampuh
    pengobatan cedera saraf tulang belakang

    ReplyDelete