Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

Atomic thread safe integer increment..

Thread view: 
lordy - 14 Sep 2006 13:04 GMT
Hi, I want to keep a running 'int' total across threads but without the
'synchronized' overhead. (This total will be spinning pretty fast).

I'm guessing Most processors have some kind of atomic (and hopefully) thread safe increment command.
However a big of Googling suggests this may be possible in Java 1.5 but
not in Java 1.4.2 ('Twas an IBM article http://www-128.ibm.com/developerworks/java/library/j-jtp11234/)

I guess the most performant(?) option is to maintain separate totals and
combine them at report time?

Lordy
Thomas Kellerer - 14 Sep 2006 13:07 GMT
> Hi, I want to keep a running 'int' total across threads but without the
> 'synchronized' overhead. (This total will be spinning pretty fast).
[quoted text clipped - 5 lines]
> I guess the most performant(?) option is to maintain separate totals and
> combine them at report time?

The perfomance "penalty" of a synchronized block is relatively small.

I think there is an article on DeveloperWorks that de-mystifies the
common believe that synchronize will make things slower. If I recall the
article correctly this was true for JDK up to 1.3 but starting with 1.4
(or 1.5 not 100% sure about that) the overhead is so small that it does
not justify the effort to avoid it.

Thomas

Signature

It's not a RootKit - it's a Sony

Thomas Hawtin - 14 Sep 2006 14:04 GMT
>> Hi, I want to keep a running 'int' total across threads but without the
>> 'synchronized' overhead. (This total will be spinning pretty fast).

>> I guess the most performant(?) option is to maintain separate totals and
>> combine them at report time?
[quoted text clipped - 6 lines]
> (or 1.5 not 100% sure about that) the overhead is so small that it does
> not justify the effort to avoid it.

If it's a contended lock, particularly on a multicore machine, it could
well be a problem. Having said that, in this case the lock shouldn't be
held for very long.

A local value for each thread should be much faster. Alternatively, if
you can live with an inaccurate variable, just use volatile (I think
that works in 1.4).

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Patricia Shanahan - 14 Sep 2006 14:10 GMT
>> Hi, I want to keep a running 'int' total across threads but without the
>> 'synchronized' overhead. (This total will be spinning pretty fast).
[quoted text clipped - 15 lines]
> (or 1.5 not 100% sure about that) the overhead is so small that it does
> not justify the effort to avoid it.
...

There is an inherent cost to synchronization in forcing threads to run
serially rather than in parallel, but you would have to be doing a
simple integer increment very, very frequently relative to other work
before that would be a problem.

The usual advice, start simple and optimize if necessary, applies.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.