Learn Java
import com.learnjava.advancedjava.Multithreading;

Multithreading in Java

@Concept

A thread is an independent path of execution within a single running program, and multithreading lets several of them run concurrently, genuinely in parallel on a multi-core machine, sharing the same process's memory. Creating one means either extending Thread and overriding its run() method, or, more commonly, implementing Runnable, a functional interface with just one abstract method, and passing it to a new Thread. Calling start() actually launches a new thread to run that code concurrently, while calling run() directly would just execute it synchronously on the current thread instead, defeating the entire point.

Because threads share the same memory, two threads modifying the same shared data at the same time, with no coordination, is a race condition — a genuine, serious bug where the final result depends unpredictably on the precise timing of each thread's execution. That's exactly the problem thread synchronization, its own topic, exists to solve.

@HowItWorks
1

What a thread is

An independent path of execution within a single running program.

2

Extending Thread

One way to create a thread, overriding its run() method with the code that should execute concurrently.

3

Implementing Runnable

The more common way, since it's a functional interface that can be passed directly to a new Thread's constructor.

4

start() versus run()

start() actually launches a new thread to run the code concurrently; calling run() directly just executes it synchronously on the current thread instead.

5

The risk of shared memory

Two threads modifying the same data simultaneously, with no coordination, creates a race condition with an unpredictable result.

@Example
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
Thread worker = new Thread(() -> {
System.out.println("Running in a separate thread");
});
worker.start();
worker.join();
System.out.println("Main thread continues after worker finishes");
}
}
Output
Running in a separate thread
Main thread continues after worker finishes
1new Thread(() -> ...) wraps a Runnable, a lambda here, describing the code that should run concurrently.
2worker.start() actually launches a new thread, running that code separately from the main thread.
3worker.join() makes the main thread wait until worker has fully finished before continuing past that line.
4Both println calls run, in this order, since join() guarantees the worker's output appears before the main thread's own final line.
@UseCases
Running a long-running task, like a file download, without blocking the rest of the program
Performing several independent computations concurrently on a multi-core machine
Understanding why calling run() directly, instead of start(), doesn't actually create a new thread
Recognizing a race condition as two threads unpredictably modifying the same shared data at once

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.