
Java Volatile vs Synchronized vs Atomic and When to Use Each
Decide between volatile, synchronized and atomic classes for shared state, with the counter example that shows why volatile alone is not enough.
Tag
66 posts

Decide between volatile, synchronized and atomic classes for shared state, with the counter example that shows why volatile alone is not enough.

Compare the G1, ZGC, Shenandoah, Parallel and Serial collectors on pauses, throughput and footprint, and pick one with a test on your own workload.

Understand autoboxing in Java: primitives versus wrappers, the Integer cache, NullPointerException on unboxing, equality traps and memory cost.

Use ExecutorService correctly: choose a pool type, submit Runnable and Callable, handle Future results and exceptions, bound the queue and shut down cleanly.

Turn on unified GC logging, read a G1 log line field by field and recognize the patterns for leaks, undersized heaps, long pauses and allocation spikes.

Use jcmd, jstack and jmap on a live Java process without causing an outage: which commands pause the JVM, how to attach in containers and what to capture.

Prevent XML external entity attacks in Java: disable DOCTYPE and external entities on DocumentBuilder, SAX, StAX, TransformerFactory and XMLReader.

Size a Java thread pool with the cores times wait over compute formula, split CPU and IO pools, measure, and consider virtual threads.

Build and test a Maven Java project on every push with GitHub Actions, using setup-java, a JDK matrix, test reports and a job that fails on broken tests.

Prevent insecure deserialization in Java: avoid ObjectInputStream on untrusted data, use serial filters, safe formats like JSON and gadget chain awareness.

Debug classpath problems in Java: find which jar a class came from, duplicate classes, version conflicts, class loaders and load logging.

Build a custom Java runtime with jlink that contains only the modules your app needs, and package it into a small Docker image with a multi-stage build.