Process vs Thread java

Multiprocessing:

Multiprocessing represents a type of multitasking which is based upon processes. Context switching in Multiprocessing, is done in-between processes. For example: Listening music, downloading file, Typing in notepad etc from internet at the same time. You can clearly identify that all applications like music software, notepad etc are independent. Multiprocessing is handled at OS level.

Process:

A process has a it’s own execution environment i.e. separate memory area will be allocated to each process. Context switch time between processes is more because switching will be done between different memory areas.

Multithreading:

Multithreading represents a type of multitasking which is based upon threads. Context switching in Multithreading, is done in-between threads. In multithreading, multiple independent tasks will execute simultaneously and these tasks will be the part of same program or application. Multithreading is handled at application or program level.

Thread:

A thread refers to a lightweight process. A thread will use the process’s memory area or execution environment. Context switch time between threads is less because switch is done within the same process’s execution environment or memory area.

A thread can be consider as a flow of execution representation. Every thread has a task or job associated with it.

Note: A thread cannot be exist without a process, it exist with-in the process.

Difference between process and thread in java:

                       Process                          Thread
  1. Process represents a program or application.
  2. It takes more time to terminate as compared to thread.
  3. Process has it’s own execution environment or main memory for execution.
  4. Process represents a heavyweight component.
  5. One process can have one or more threads.
  6. In case of process, context switch time is more as compared to thread
  7. Resources as per Process:
    • Address space
    • Global variables
    • Open files
    • Child processes
    • Pending alarms
    • Signals and signal handlers
    • Accounting information
  1. Thread represents a part or segment of a program or application.
  2. It takes less time to terminate as compared to process.
  3. Thread use process’s execution environment or main memory for execution. Same process memory can be shared with other threads also.
  4. Thread represents a lightweight component.
  5. One thread can only have one process, it cannot have multiple processes.
  6. In case of thread, context switch time is less as compared to process.
  7. Resources as per thread:
    • Program counter
    • Registers
    • Stack
    • State

Thread Example

class Test extends Thread{
    public void run( ){
        for(int i = 3; i > 0; i--){
            System.out.println("Test class thread is running: " +i);
        }
    }
}
public class Main extends Thread{
    public void run( ){
        for(int i = 3; i > 0; i--){
            System.out.println("Main class thread is running: " +i);
        }
    }
 
    public static void main(String args[ ]){
        Test test = new Test();
        Main main = new Main();
 
        test.start();
        main.start();
    }
}

Output

Test class thread is running: 3
Test class thread is running: 2 
Test class thread is running: 1 
Main class thread is running: 3 
Main class thread is running: 2
Main class thread is running: 1

Java interview questions on multithreading

Please Share