1. 7

  2. 5

    Summary: stamped locks, concurrent adders, new date API, a less pathetically poor Process API, overflow-trapping numeric operations (as new functions), secure random generation, and an Option/Maybe type.

    The new date/time API is going to help me a lot with my current project when we switch to Java 8. The process API thing might make it reasonable to invoke external utilities from Java, although I’m reserving judgment. Beyond that, these seem kind of niche (because they’re limited due top backward-combatibility, often), although important in their niches.

    1. 3

      With things like the ConcurrentAdder I wonder why its a new API instead of just a new, faster implementation of AtomicInteger. The author supposes that developers should always use the adder, so why the new API?

      1. 4

        In addition to the size argument made in a sibling comment, AtomicInteger/Long has a very different API to LongAdder. It is used for counter management because it’s the best construct available for it, but that doesn’t mean it’s a great one.

        In particular you always sample when you increment an atomic type (using incrementAndGet or getAndIncrement). This means it must update inline. The adder type, which has a void increment, doesn’t have this constraint, so can defer the synchronisation cost. The actual means of implementation changes the structure’s internal representation enough that it warrants another type.

        1. 3

          One potential problem (if it matters to you) is size: LongAdder is a lot bigger. It uses a list of packed AtomicLongs. So if you have 24 cores and high contention, your LongAdder could grow to 24 packed counters. And each counter is packed with 64 bytes to prevent false-sharing along cache lines, which makes them rather bloaty for what is basically a simple counter.

          Of course, if you have 24 cores and high enough contention to cause the LongAdder to use 24 counters…you need the concurrency anyway and probably don’t care about the bloat.

          If your counter is rare-contended, AtomicLong will be faster since there is a certain amount of overhead to the whole LongAdder setup.