[concurrency-interest] LongAdder in JDK?

Adrian Tarau adrian.tarau at gmail.com
Fri Dec 9 14:18:34 EST 2011


One more thing(somebody asked)...Lower is better since we measure avg 
time spent in increment().

Thanks,
Adrian Tarau.

On 12/09/2011 02:12 PM, Adrian Tarau wrote:
> An just in case somebody wonders how is it the naive implementation, 
> here is the code. Also the ns(timing) column is the median 
> measurement(point 3), not really explained in the documentation.
>
> Thanks,
> Adrian Tarau.
>
> /public abstract class ConcurrentAtomicLong {
>
>     public static ConcurrentAtomicLong newInstance() {
>         return new LongAdderImpl();
>     }
>
>     public abstract void increment();
>
>     public abstract long get();
>
>     protected static class StrippedAtomicLongImpl extends 
> ConcurrentAtomicLong {
>
>         private final static int THREAD_ID_BITS_USED = 5;
>         private final static int THREAD_ID_BITS_SIZE = 1 << 
> THREAD_ID_BITS_USED;
>         private final static int THREAD_ID_BITS_MASK = 
> THREAD_ID_BITS_SIZE - 1;
>
>         private final AtomicLong[] instances = new 
> AtomicLong[THREAD_ID_BITS_SIZE];
>
>         {
>             for (int index = 0; index < instances.length; index++) {
>                 instances[index] = new AtomicLong(0);
>             }
>         }
>
>         static int hash(int h) {
>             h ^= (h >>> 20) ^ (h >>> 12);
>             return h ^ (h >>> 7) ^ (h >>> 4);
>         }
>
>         @Override
>         public void increment() {
>             int bucket = hash((int) Thread.currentThread().getId()) & 
> THREAD_ID_BITS_MASK;
>             instances[bucket].incrementAndGet();
>         }
>
>         @Override
>         public long get() {
>             long sum = 0;
>             for (AtomicLong instance : instances) {
>                 sum += instance.get();
>             }
>             return sum;
>         }
>     }
>
>     protected static class AtomicLongImpl extends ConcurrentAtomicLong {
>
>         private final AtomicLong instance = new AtomicLong(0);
>
>         @Override
>         public void increment() {
>             instance.incrementAndGet();
>         }
>
>         @Override
>         public long get() {
>             return instance.get();
>         }
>     }
>
>     protected static class LongAdderImpl extends ConcurrentAtomicLong {
>
>         private final LongAdder instance = new LongAdder();
>
>         @Override
>         public void increment() {
>             instance.increment();
>         }
>
>         @Override
>         public long get() {
>             return instance.sum();
>         }
>     }
> }/
>
> On 12/09/2011 02:06 PM, Adrian Tarau wrote:
>> Sorry, forgot to include some Coliper documentation about how to read 
>> these graphs: 
>> http://code.google.com/p/caliper/wiki/OnlineResults?show=content
>>
>> Thanks,
>> Adrian Tarau.
>>
>> On 12/09/2011 11:00 AM, Adrian Tarau wrote:
>>> This chart shows you why you should use JDK classes before thinking 
>>> you can outsmart the default implementations :)
>>> StrippedAtomicLong is my naive implementation of a LongAdder using a 
>>> fixed number of AtomicLong instances and the masked thread id as the 
>>> index(using 64 AtomicLongs - best performance, increasing this 
>>> number doesn't help for maximum 20 threads).
>>>
>>> This chart was created using Caliper <http://code.google.com/p/caliper>.
>>>
>>>
>>>
>>> On 12/08/2011 04:38 PM, Doug Lea wrote:
>>>> On 12/08/11 15:04, Adrian Tarau wrote:
>>>>> There are any plans to include LongAdder in JDK
>>>>
>>>> Yes. Everything in package jsr166e is a candidate for JDK8.
>>>>
>>>>> Is this the stable version of the LongAdder?
>>>>> http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/?pathrev=MAIN 
>>>>>
>>>>
>>>> Yes. Comments, experience reports, etc are welcome.
>>>>
>>>> -Doug
>>>> _______________________________________________
>>>> Concurrency-interest mailing list
>>>> Concurrency-interest at cs.oswego.edu
>>>> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>>>
>>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20111209/36c77cf8/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 89637 bytes
Desc: not available
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20111209/36c77cf8/attachment-0001.png>


More information about the Concurrency-interest mailing list