Thread lifecycle
A thread moves through states: NEW after creation, RUNNABLE when executing, BLOCKED waiting for a lock, WAITING for a signal, and TERMINATED when done. The scheduler decides when runnable threads actually run on a CPU.
Avoiding deadlocks
A deadlock happens when threads wait on each other forever. Avoid by always acquiring locks in the same order, using timeouts with tryLock, or preferring lock-free constructs:
import java.util.concurrent.atomic.AtomicInteger;
AtomicInteger count = new AtomicInteger(0);
count.incrementAndGet(); // thread-safe without synchronized
AtomicInteger and similar atomic classes use hardware-level operations to stay safe without explicit locks.