Skip to main content

Threads and Processes: What's the difference?

A Process:

A process is the carrying out of a program that allows you to perform the program's actions specified. The operating system helps you to schedule, create, and terminate the processes that are used by the CPU. When a process is loaded into memory, it contains all the resources required to perform the actions necessary. These resources include mainly registers, a program counter and a stack. The register holds the data, such as instructions needed by the Process, in the CPU. There is a program counter to keep track of where the computer program is with completing the actions. Finally, a stack holds the data of the active subroutines of the program.

A Thread:

A Thread is a unit of execution that forms part of a process. A Thread cannot contain a Process. However, a Process can have multiple Threads that are performed at relatively the same time. Threads are lightweight, as they have their own stack and are self-sufficient. Unlike Processes, if there is a problem with one of the Threads, it will affect other Threads and can cause Process issues.

Multithreading:

Multithreading refers to multiple Threads being executed by an operating system. Or simply, two or more Threads of the same Process are running simultaneously.

What's the Difference?

  • Process means a program is in execution, whereas Thread means a segment of a process.
  • A Process takes more time for creation than a Thread.
  • A Process takes more time to terminate than a Thread.
  • A Process is not Lightweight, whereas Threads are Lightweight.
  • A Process does not share data, whereas Threads share data with each other.

Popular posts from this blog

What happens if we don't override the run() method:

Consequences of not overriding the run() method: If we don’t override the run() method, the compiler will not flash any error, and it will execute the run() method of the Thread class that has been emptily implemented, So, there will be no output for this Thread. Below is an example of the method returning no output: public class Example extends Thread { public static void main(String[] args) { Thread thread = new Thread( new Example()); thread.start(); // will create a new thread and call thread class's run() method which has no implementation. } }

Creating and Running a Java Thread:

Ways to create a Thread: You can create a Thread in two ways: By extending the Thread class. By implementing a Runnable Interface. Thread class: The Thread class provides constructors and methods to create and perform operations on a thread. Runnable Interface: Runnable Interfaces have only one method,  named run() . The Runnable Interface should be performed by classes where their instances plan to be executed by a thread.  public void run() : is used to perform the action for a Thread. Starting a thread: The  start() method  of a Thread class is used to start a newly created thread. It performs the following tasks: A new thread starts(with their stack). The Thread moves to the Runnable state from New state. The  run() method  will run when the Thread gets a chance. Java Thread Example by extending Thread class: class  Multi  extends  Thread{   public   void  run(){   System.out.println( "Extended Thread Class E...

Interfaces - Need to know:

What is an Interface? An Interface is like a class in that it can have methods and variables. However, methods declared in an Interface are abstract by default, meaning they only contain the method signature, not the method body. Facts about Interfaces: Interfaces specify what the class must do and not how the class must do it. They are considered as the class blueprint. If a class implements an interface, the class must provide all the method bodies for the interface's functions. Otherwise, the class must be declared abstract. They are used to achieve total abstraction. An Interface can extend one or many Interfaces. A class can extend many Interfaces, but only one class. Nested Interfaces is when an Interface is declared inside another interface. It is also used to achieve loose coupling. Interfaces are used to implement abstraction.  Difference between Abstract Class and Interface: Differences between an Abstract class and an Interface include: An interface can have strictly abs...