Thread in Operating System
Thread in Operating System
What is a Thread?
A thread is a path of execution within a process. A process can contain multiple
threads.
Multithreading
A thread is also known as lightweight process. The idea is to achieve parallelism
by dividing a process into multiple threads.
For example, in a browser, multiple tabs can be different threads.
MS Word uses multiple threads: one thread to format the text, another thread to
process inputs, etc. More advantages of multithreading are discussed below
Process vs Thread?
The primary difference is that threads within the same process run in a shared
memory space, while processes run in separate memory spaces.
Threads are not independent of one another like processes are, and as a result
threads share with other threads their code section, data section, and OS
resources (like open files and signals). But, like process, a thread has its own
program counter (PC), register set, and stack space.
Types of Threads
There are two types of threads.
मल्टीथ्रेड ग
िं क्यों?
थ्रेड िो ऱाइ वे प्रोसेस भी िहा जाता है । किसी प्रोसेस िो बहुत सारे थ्रेड में ववभाजजत िर दे ते हैं
और स्मनान्तारीिरण िो प्राप्त िरते हैं।
उदाहरण िे सऱए किसी ब्राउज़र में बहुत सारे ै ब्स िो थ्रेड िी तरह दे खा जा सिता है ।
t1.start();
System.out.println("t1 State : " + t1.getState());
System.out.println("t2 State : " + t2.getState());
t2.start();
System.out.println("t1 State : " + t1.getState());
System.out.println("t2 State : " + t2.getState());
}
}
Output :
t1 State : NEW
t2 State : NEW
t1 State : RUNNABLE
run method
t2 State : NEW
t1 State : TERMINATED
run method
t2 State : RUNNABLE