Process vs thread

Thread #

Process #

A process is (usually) an instance of a computer program.

Processes can interact in several ways. In particular:

  • a process may spawn another process,
  • a process may wait for another process to terminate or release a lock on some resource.

Thread #

A process consists of one or several threads.

Conceptually, a thread is similar to a process. In particular, threads can interact analogously to processes.

However, threads are designed to allow heavier concurrency, thanks to a shared address space and fast context switches.

in Java #

A Java program is normally executed by a single process.

This process consists of several threads. Among these, the main thread is the one that executes the main method of the application (e.g. the method public static void main of the main class).

Programming with threads #

Many high-level programming languages (like Java) allow creating threads and assigning tasks to these threads. However, by default, a program does not specify whether two threads are executed on the same core (i.e. concurrently) or not (i.e. in a parallel fashion). The decision is usually left to the operating system, and depends on the resources available at runtime.

In particular, if a program allows two threads to be executed concurrently (on the same core), then it also allows them to be executed in parallel (on different cores).