🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Process & Thread  


Process

  • A process is a program in execution.
  • It is an independent unit that has its own memory space and resources managed by the operating system.
  • Processes are isolated from each other (one process can’t directly access another’s memory).
  • For example:
    • Text editor (Word, Notepad) – opening multiple files creates separate processes.
    • Media player – playing music/video in one app while another app runs separately.
    • Web browser (Chrome, Firefox) – each tab can run as a separate process.
    • Operating system utilities – Task Manager, antivirus scans, etc., all run as separate processes.
  • How to Achieve a Process ?
    • Processes are generally created by the operating system when you run a program.
    • In Java, you can also create new processes programmatically using:
      • Runtime.getRuntime().exec() → executes system commands.
      • ProcessBuilder class → more advanced control over process creation and I/O.
    • Example in Java:
      import java.io.IOException;
      
      public class ProcessExample
      {
          public static void main(String[] args) throws IOException
          {
              Process process = Runtime.getRuntime().exec("notepad.exe"); // opens Notepad
          }
      }
  • Other Important Points
    • Each process has a unique process ID (PID).
    • Processes can be foreground (active) or background (running in the background).
    • Communication between processes can be done using Inter-Process Communication (IPC).
    • Process scheduling is handled by the Operating System to allocate CPU time efficiently.
    • Processes are heavier than threads because each process has its own memory, while threads share memory.
    • A process can consist of one or more threads, but it is considered the basic unit of execution in an operating system.

Thread

  • A thread is the smallest unit of execution within a process.
  • Multiple threads can exist inside one process, and they share the same memory space and resources of that process.
  • Threads run concurrently to perform different tasks in parallel, improving performance and responsiveness.
  • If one thread crashes, usually the whole process is affected (since they share memory).
  • For example:
    • Microsoft Word / Notepad – one thread handles the UI (typing, menus), another checks spelling/grammar, and another handles auto-save.
    • VLC Media Player – one thread decodes video frames, another decodes audio, another handles I/O (reading file/stream), and another draws video to the screen.
    • Web Browser Tab – inside a tab process, multiple threads work together: one for rendering the page, another for JavaScript execution, another for network requests.
    • Operating System – in the background, threads manage keyboard input, mouse events, file system access, etc., all inside larger OS processes.
  • How to Achieve a Thread ?
    • Threads are created inside a process and share the same memory and resources of that process.
    • In Java, we can create threads in two main ways:
      1. Using Thread class
      2. Using Runnable interface
    • Click Here for programs.
  • Other Important Points
    • Each thread has a unique thread ID within a process.
    • Threads within the same process share the same memory and resources, making communication faster than processes.
    • Thread scheduling in Java is handled by the JVM and the underlying Operating System.
    • Threads are lighter than processes because they don’t need separate memory; they use the memory of the parent process.

Difference between Process & Thread

  • Below are some difference between Process and Thread in Java.
Aspect Process Thread
Definition A process is a program in execution. A thread is the smallest unit of execution within a process.
Memory Each process has its own memory space. Threads share the memory of the process they belong to.
Overhead Processes are heavier and require more resources. Threads are lighter and more efficient in context switching.
Isolation Processes are isolated; one process cannot directly access another’s memory. Threads are not isolated; they share data within the same process.
Communication Inter-Process Communication (IPC) is slower. Communication between threads is faster since they share memory.
Creation in Java Created using Runtime.getRuntime().exec() or ProcessBuilder. Created by extending Thread or implementing Runnable.
Failure If a process crashes, it does not affect other processes. If a thread crashes, it can bring down the whole process.
Basic Unit Process is the basic unit of resource allocation. Thread is the basic unit of execution.