Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Atomic thread safe integer increment..

Reply
Thread Tools

Atomic thread safe integer increment..

 
 
lordy
Guest
Posts: n/a
 
      09-14-2006
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/developerwork...ry/j-jtp11234/)

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

Lordy
 
Reply With Quote
 
 
 
 
Thomas Kellerer
Guest
Posts: n/a
 
      09-14-2006
On 14.09.2006 14:04 lordy wrote:
> 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/developerwork...ry/j-jtp11234/)
>
> 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


--
It's not a RootKit - it's a Sony
 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      09-14-2006
Thomas Kellerer wrote:
> On 14.09.2006 14:04 lordy wrote:
>> 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?
>>

>
> 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.


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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      09-14-2006
Thomas Kellerer wrote:
> On 14.09.2006 14:04 lordy wrote:
>> 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/developerwork...ry/j-jtp11234/)
>>
>> 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.

....

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] atomic 0.0.1 - An atomic reference for Ruby Charles Oliver Nutter Ruby 5 06-08-2010 01:04 PM
Re: is assignment atomic/thread safe? Tom Anderson Java 5 02-22-2009 04:53 AM
Re: is assignment atomic/thread safe? Mark Thornton Java 24 02-21-2009 01:07 AM
Atomic operations and Thread safe code blackstreetcat@hotmail.com C Programming 6 03-26-2006 12:28 AM
Atomic operations and Thread safe code blackstreetcat@hotmail.com C Programming 0 03-25-2006 02:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57