Multitasking: Multiprocessing vs Multithreading

Naveen AnamaneniUpdated · 3 min read

Multitasking is the ability of a computer to run several tasks at the same time, such as playing music while you type a document. There are two ways to multitask. Process-based multitasking (multiprocessing) runs several separate programs at once. Thread-based multitasking (multithreading) runs several tasks inside one program. Java programs mainly use multithreading. In this lesson, you will learn the differences, and the related ideas of concurrency and parallelism.

Process-Based vs Thread-Based Multitasking

MultiprocessingMultithreading
Unit of workA process (program)A thread inside a process
MemoryEach process has its ownThreads share their process's heap
Switching costHighLow
CommunicationSlow (pipes, sockets, files)Fast (shared objects)
IsolationStrong: one crash rarely affects othersWeak: shared data can be corrupted
ExampleBrowser, editor and music player running togetherA web server handling many requests in one JVM

Concurrency vs Parallelism

Concurrency takes turns; parallelism truly runs at the same time
  • Concurrency means several tasks are in progress at the same time. On one CPU core, the system switches between them quickly, so they take turns.
  • Parallelism means several tasks run at the same instant on different CPU cores.

Java threads give you concurrency on any machine, and parallelism when the computer has more than one core.

Example: How Many Cores?

Let's see an example that asks the JVM how many processors it can use. The number depends on the computer, so the output is a sample.

Cores.java▶ Try it
public class Cores {
    public static void main(String[] args) {
        int cores = Runtime.getRuntime().availableProcessors();
        System.out.println("Available processors: " + cores);
        System.out.println("Current thread: "
            + Thread.currentThread().getName());
        System.out.println("Live threads in this JVM: "
            + (Thread.activeCount() >= 1));
    }
}
Output
Available processors: 10
Current thread: main
Live threads in this JVM: true

Explanation: availableProcessors() tells you how many threads can truly run in parallel. Every program runs in at least one thread, main. The JVM also runs its own background threads, such as the garbage collector.

Common Mistakes

Thinking more threads always means faster. Beyond the number of cores, CPU-heavy threads just take turns.

Confusing concurrency with parallelism. Concurrency is about structure; parallelism needs multiple cores.

Practice

  1. Easy: Print the number of available processors and explain what it means for a program that sorts big arrays.
  2. Harder: Start as many threads as there are processors, each printing its name once, and join them all.

Quick check: Multitasking

4 questions
1Which kind of multitasking runs several tasks inside one program?
2Do separate processes share memory?
3Two tasks take turns on one core. Is that concurrency or parallelism?
4Which method reports the number of usable processors?

Key Takeaways

Was this tutorial helpful?

Lesson 378 of 482