File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Atomic Integer Vs increment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 1371
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: 18486
    
  40

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: 2158
    
  47
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Atomic Integer Vs increment
 
Similar Threads
Multi-Threaded access primitive Array / Volatile
Aomic Integer
i++ is not a singular operation"?
Synchronize on int shouldn't be necessairy, should it?
WA #1.....word association