• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Atomic Integer Vs increment

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which give better performnace for counter(how many hits for my site), Atomic Integer or increment (++)
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:Which give better performnace for counter(how many hits for my site), Atomic Integer or increment (++)



First, you do know that one is thread safe and the other isn't right? Under single threaded conditions, it's kinda not a fair comparison. And under multithreaded conditions, well, one isn't guaranteed to work correctly.

Henry
 
Ranch Hand
Posts: 30
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:Which give better performance for counter(how many hits for my site), Atomic Integer or increment (++)



If you have multithreads, use AtomicInteger, if not, use the common increment.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:Which give better performance for counter


What do you mean by better performance, is it accuracy, speed or something else altogether?

Pablo Abbate wrote:If you have multithreads, use AtomicInteger, if not, use the common increment.


I think the decision is a bit more complex than that.
It may be that the reads and writes are currently done from within synchronized blocks for other reasons we are not aware of or it maybe that the hit counter is only indicative and so (providing writes are done from within a synchronized block) occasionally being out by a few hits on reads may not be an issue.
 
Pablo Abbate
Ranch Hand
Posts: 30
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:

kri shan wrote:Which give better performance for counter


What do you mean by better performance, is it accuracy, speed or something else altogether?

Pablo Abbate wrote:If you have multithreads, use AtomicInteger, if not, use the common increment.


I think the decision is a bit more complex than that.
It may be that the reads and writes are currently done from within synchronized blocks for other reasons we are not aware of or it maybe that the hit counter is only indicative and so (providing writes are done from within a synchronized block) occasionally being out by a few hits on reads may not be an issue.



I mean if the counter is being (or can be) updated with multiple threads.
If the counter is inside a synchronized block, you don't have multithreads accessing the counter.
reply
    Bookmark Topic Watch Topic
  • New Topic