
Java Atomic LongAdder and AtomicInteger for Safe Counters
Use AtomicInteger, AtomicLong and LongAdder for thread safe counters in Java: compareAndSet, updateAndGet, contention behaviour and which class to choose.
Tag
66 posts

Use AtomicInteger, AtomicLong and LongAdder for thread safe counters in Java: compareAndSet, updateAndGet, contention behaviour and which class to choose.

Move a Java service to generational ZGC: the flags by JDK version, what changes from G1 and single generation ZGC, heap headroom and how to compare results.

Fix java.lang.StackOverflowError by finding the missing base case or the hidden cycle, then convert deep recursion to a loop or raise the thread stack size.

Compare an anemic domain model with a rich one in Java: where the rules live, an order example in both styles, tests, persistence and a practical way to move.

Shut down a Java ExecutorService properly with shutdown, awaitTermination and shutdownNow, handle interrupts, and avoid thread leaks in web apps and tests.

Size the Java heap in a container from measured numbers: live set after GC, non heap memory, the container limit and MaxRAMPercentage.

Use Arthas to diagnose a running JVM without restarting it: attach, find hot threads, trace slow methods, watch arguments and decompile loaded classes safely.

Decode the Note about unchecked or unsafe operations, find the lines with Xlint, and fix raw types and casts instead of suppressing the warning.

Prevent server side request forgery in Java: allowlists for outbound URLs, blocking private and metadata addresses, DNS rebinding and redirect handling.

Write a ThreadFactory that names threads, sets daemon status and an uncaught exception handler, so dumps and logs show which pool a thread is in.

How the JVM reads container limits: MaxRAMPercentage, InitialRAMPercentage, MinRAMPercentage, availableProcessors and how to confirm what the JVM sees.

Set up Java remote debugging with the JDWP agent, connect from jdb or an IDE, reach a container or Kubernetes pod safely and avoid exposing the port.