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. } }

What is Deadlock, Livelock and Starvation?

Deadlock, Livelock and Starvation: Deadlock: Deadlock   is where a set of processes are blocked because each process is holding a resource and waiting for another resource that is doing the same thing.  An example of this is when two trains are coming toward each other on the same track. Neither of the trains can move once they are in front of each other (assuming there are no connecting tracks).  Livelock: Livelock  occurs when two or more processes continue to repeat in response to changes in other processes without achieving any productive outcome. These processes do not get placed in the waiting state but continue to run without any productive outcome. This differs from a deadlock because, in a deadlock, all processes are put in the waiting state. Starvation: Starvation is a problem that is closely related to both Livelock and Deadlock. This can occur as a policy is used to determine who gets access to the resource when it can lead to some processes never getting...

Executer vs ExecuterSevice: What's the Difference?

Executer and ExcuterService: Executor and ExecutorService are part of Java's Executor framework, which provides  thread pool  facilities to Java applications. Since creating and managing Threads are expensive, It's a better idea to create a thread pool that can execute in parallel rather than every time a new thread is requested, helping reduce the load. This improves the response time of the application. Differences between Executer and ExecuterService: One of the key differences between the Executor and ExecutorService interface is that the former is a parent interface while ExecutorService extends Executor.  A second significant difference between ExecutorService and Executor is that Executor defines an execute() method, which accepts the Runnable interface object. Simultaneously, a submit() method takes both Runnable and Callable object interfaces. Another difference between Executor and ExecutorService interface is that the execute() methods' return type is void, wh...