4

Currently, my program makes heavy use of AtomicIntegers in order to keep track of running maximums of ints that are reported in parallel.

After reading about Java 8's new LongAccumulators, I had hoped to use an IntAccumulator with max as the accumulator function in order to accomplish my desired behavior with less thread contention. However it doesn't seem to exist.

Is there some reason that an IntAccumulator wouldn't make sense? It seems plausible to me that there could be low level instructions that make an IntAdder unnecessary, but an IntAccumulator could represent more behaviors than can be captured by a low level instruction.

(If it's relevant at all, I intend to use these from Scala code)

Mark Pierotti
  • 141
  • 1
  • 6
  • Perhaps they didn't deem it useful to have `Int` versions of the classes. You don't see a `ShortAccumulator` or `ShortAdder` either. – Kayaman May 03 '17 at 09:38
  • 2
    Well, __do__ you really need it? Why not just use `Long::max` instead as in `LongAccumulator accumulator = new LongAccumulator(Long::max, 0);`? Then use it with regular integers like `accumulator.accumulate(10);`. As a side note, I dug through a mailthread (http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-January/013388.html), but no mention about the design decision. – Magnilex May 03 '17 at 12:53
  • @Magnilex I agree that it's not strictly necessary, but I had been hoping to not incur a performance penalty for switching to Longs. – Mark Pierotti May 03 '17 at 16:41
  • 1
    I don't believe there would be a performance difference at the instruction level or a memory savings by using ints instead. I think that the shared nature of the counters, object alignment, cache block size, etc. would make the 32-bit and 64-bit equivalent on x86-64. – Ben Manes May 04 '17 at 03:41

0 Answers0