Java

Multithreading

32 lessons in this section of the Java tutorial. Work through them in order, or jump to the one you need.

  1. 1.Process vs Thread

    A process is an independent program in execution with its own isolated memory space, file handles, and system resources, managed by the operating system and separated…

  2. 2.Thread Basics

    A Java thread is an instance of java.lang.Thread that represents an independent path of execution within the JVM process.

  3. 3.Creating Threads

    Java provides three primary abstractions for defining the work a thread will execute: the Thread class itself (subclassed to override run()), the Runnable interface (a…

  4. 4.Thread Lifecycle

    The Java thread lifecycle is the complete sequence of states a thread passes through from the moment a Thread object is constructed to the moment its execution ends.

  5. 5.Thread Priority

    Thread priority in Java is an integer hint to the OS scheduler indicating the relative importance of a thread compared to others.

  6. 6.Thread Scheduler

    The thread scheduler is the component of the operating system responsible for deciding which RUNNABLE thread executes on which CPU core at any given moment.

  7. 7.Synchronization

    Synchronization in Java is the mechanism by which the JVM enforces mutual exclusion and visibility across threads.

  8. 8.synchronized Keyword

    The synchronized keyword is Java's built-in mechanism for declaring that a block of code or an entire method must execute under a monitor lock.

  9. 9.Inter-thread Communication

    Inter-thread communication is the mechanism by which Java threads coordinate their activities — not just prevent interference, but actively signal each other about state…

  10. 10.wait()

    wait() is an instance method on Object that causes the current thread to release the object's monitor lock and suspend until another thread calls notify() or notifyAll()…

  11. 11.notify()

    notify() is an instance method on Object that wakes exactly one thread from the object's wait set — the set of threads currently blocked in wait() on that object.

  12. 12.notifyAll()

    notifyAll() wakes all threads currently in the object's wait set — every thread that has called wait() on that object and has not yet been woken.

  13. 13.Deadlock

    Deadlock is a situation in which two or more threads are permanently blocked, each waiting to acquire a lock held by another thread in the same cycle.

  14. 14.Race Condition

    A race condition is a defect in concurrent code where the correctness of a computation depends on the relative timing or interleaving of operations in multiple threads.

  15. 15.Starvation

    Starvation is a liveness failure in which a thread is perpetually denied access to a resource it needs to make progress, not because of deadlock (no cycle of blocked…

  16. 16.Thread Safety

    A class is thread-safe if it behaves correctly when accessed from multiple threads simultaneously, regardless of the scheduling or interleaving of those threads, without…

  17. 17.volatile

    The volatile keyword in Java is a field modifier that provides two guarantees: visibility and ordering.

  18. 18.Atomic Variables

    The java.util.concurrent.atomic package provides a set of classes that support lock-free, thread-safe operations on single variables: AtomicBoolean, AtomicInteger…

  19. 19.Executors Framework

    The Executors framework, introduced in Java 5 as part of java.util.concurrent, provides a high-level abstraction layer over raw thread management.

  20. 20.Thread Pool

    A thread pool is a managed collection of pre-created, reusable worker threads that execute submitted tasks from a shared work queue, eliminating the overhead of creating…

  21. 21.Future

    Future<V> is a Java interface that represents the result of an asynchronous computation — a value that may not yet be available but will be at some point in the future.

  22. 22.CompletableFuture

    CompletableFuture<T>, introduced in Java 8, is a Future implementation that supports manual completion, non-blocking callbacks, and a rich set of composition and…

  23. 23.ForkJoinPool

    ForkJoinPool is an ExecutorService implementation designed specifically for divide-and-conquer parallelism: splitting a large task into smaller subtasks recursively…

  24. 24.Locks API

    The java.util.concurrent.locks package, introduced in Java 5, provides a richer and more flexible locking framework than Java's built-in synchronized keyword.

  25. 25.ReentrantLock

    ReentrantLock is the primary Lock implementation in java.util.concurrent.locks and the direct replacement for synchronized when more control over locking is needed.

  26. 26.ReadWriteLock

    ReadWriteLock is an interface in java.util.concurrent.locks that maintains a pair of associated locks — one for read-only operations and one for write operations.

  27. 27.Semaphore

    A Semaphore maintains a set of permits. Threads acquire permits before proceeding and release them when done, allowing at most N threads to access a resource…

  28. 28.CountDownLatch

    CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of operations being performed by other threads completes.

  29. 29.CyclicBarrier

    CyclicBarrier is a synchronization aid that allows a fixed number of threads to wait for each other at a common barrier point, then all proceed simultaneously.

  30. 30.Phaser

    Phaser is the most flexible synchronization barrier in java.util.concurrent, introduced in Java 7 as a generalization of both CountDownLatch and CyclicBarrier.

  31. 31.Parallel Streams

    Parallel streams execute stream operations concurrently using the common ForkJoinPool, transparently splitting the data source into chunks and processing each chunk on a…

  32. 32.Virtual Threads

    Virtual threads, introduced as a preview in Java 19 and finalized in Java 21 as part of Project Loom, are lightweight threads managed by the JVM rather than the OS.