Threads & Synchronization in Java

From the Java cheat sheet · Concurrency · verified Jul 2026

Threads & Synchronization

Creating threads and guarding shared state.

java
// Quick Reference
Thread t = new Thread(() -> System.out.println("run"));
t.start();
t.join();                 // wait for it to finish
synchronized void inc() { count++; }
💡 Call start() (not run()) to launch a new thread; run() executes on the current one.
⚡ synchronized prevents race conditions on shared mutable state.
📌 volatile ensures a written value is visible to other threads, but is not atomic.
🟢 Thread.sleep and join throw InterruptedException - handle or declare it.
threadssynchronized

More Java tasks

Back to the full Java cheat sheet