visit
A program is nothing but a set of instructions.
Let's say we are on a Windows OS; we open our notepad to write a simple "Hello World" program. Do you see what I did there? Let's write this program in Java since it is the most known language, don't worry if you are unfamiliar with it; the syntax is similar to C++.
public class HelloWord {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
Notice the file name should be the same as the class name for it to work. you can run it using the command line, java HelloWorld.java
, and you'll be greeted with the following output.
Hello World
Unless you run the HelloWorld.java
file, it is called a program.
A program is nothing but a set of instructions.
A process is nothing but a program in execution.
A thread is a segment of a process. It is sometimes referred to as a mini process or process within a process.
It can be challenging to wrap one's head around this, but let's take an example to understand why a process can have multiple threads and their advantage over a single thread.
Suppose you are writing a letter to your friend on a word processor. The word processor itself is a running program (i.e., a process). Let's say this process is running with three threads.
Thread One: Respond to the user when he types (Interactive Thread)
Thread Two: Saves the file to the disk periodically, RAM to DISK (Background Thread)
Thread Three: Render the page according to the changes made. (Computational Thread).
Well, because all three threads operate on the same document. Threads share the same memory within a process. By having three threads, they share a common memory, and thus all have access to the edited document.