Fundamentals 10 min read

Master Java Multithreading: Why, How, and Common Pitfalls Explained

This article introduces the fundamentals of Java multithreading, explaining why concurrency is needed, the differences between programs, processes and threads, four ways to create threads, and common pitfalls such as thread safety and deadlocks, all with clear code examples.

macrozheng
macrozheng
macrozheng
Master Java Multithreading: Why, How, and Common Pitfalls Explained

Why Use Multithreading

Multithreading is introduced to solve the problem of handling many simultaneous requests, which is common in modern high‑traffic services such as e‑commerce flash sales. It improves execution efficiency but brings thread‑safety challenges, e.g., the differences between HashMap and Hashtable, or Vector and ArrayList. Locks can prevent data races but may cause deadlocks.

Process vs Thread

Program

A program is a collection of code or instructions, such as WeChat.exe , which must be loaded into the CPU to run.

Process

When a program runs, it becomes a process . A process is an active entity with its own memory space, while a program is static.

On a single‑core CPU only one process runs in a time slice, but rapid context switching creates the illusion of parallelism; on multi‑core CPUs true parallel execution occurs.

Each process has its own virtual memory that abstracts the physical memory. When physical memory is exhausted, the OS swaps parts of virtual memory to disk, which slows down execution.

Thread

A thread is the actual execution path inside a process. Threads share the process’s heap but each has its own stack.

How to Create Threads in Java

Java provides the java.lang.Thread class and the Runnable interface. The official documentation lists two primary ways to create a thread:

Extend the Thread class and override run(), then start the thread with start().

Implement the Runnable interface, pass an instance to a new Thread, and call start(). This is effectively a proxy pattern.

Extending Thread Class

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("小齐666:" + i);
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();
        for (int i = 0; i < 100; i++) {
            System.out.println("主线程" + i + ":齐姐666");
        }
    }
}

The main method runs in the main thread, creates a new MyThread instance, and starts it. Both threads execute concurrently, producing interleaved output.

Implementing Runnable Interface

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            System.out.println("小齐666:" + i);
        }
    }

    public static void main(String[] args) {
        new Thread(new MyRunnable()).start();
        for (int i = 0; i < 100; i++) {
            System.out.println("主线程" + i + ":齐姐666");
        }
    }
}

This approach is more flexible because Java only allows single inheritance; implementing Runnable lets a class extend another superclass if needed.

Conclusion

Both techniques ultimately invoke start(), which creates a new OS thread and then calls the overridden run(). Directly calling run() merely executes the method in the current thread and does not start a new thread. Using Runnable is generally preferred for its flexibility, and thread pools built on these primitives are the standard way to manage many concurrent tasks in production systems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaconcurrencyprogrammingmultithreadingprocess
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.