aspose file tools
The moose likes Beginning Java and the fly likes Atomic Integer Vs increment Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Atomic Integer Vs increment" Watch "Atomic Integer Vs increment" New topic
Author

Atomic Integer Vs increment

kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1303
Which give better performnace for counter(how many hits for my site), Atomic Integer or increment (++)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16811
    
  19

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
Pablo Abbate
Ranch Hand

Joined: Aug 06, 2012
Posts: 30

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.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1223
    
    3
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

Joined: Aug 06, 2012
Posts: 30

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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Atomic Integer Vs increment
 
Similar Threads
Multi-Threaded access primitive Array / Volatile
WA #1.....word association
Synchronize on int shouldn't be necessairy, should it?
i++ is not a singular operation"?
Aomic Integer