Multithreading in Java
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.
What a thread is
An independent path of execution within a single running program.
Extending Thread
One way to create a thread, overriding its run() method with the code that should execute concurrently.
Implementing Runnable
The more common way, since it's a functional interface that can be passed directly to a new Thread's constructor.
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.
The risk of shared memory
Two threads modifying the same data simultaneously, with no coordination, creates a race condition with an unpredictable result.
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"); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.