CSE Module 5
CSE Module 5
1 It doesn’t block the user because threads are independent and you
can perform multiple operations at the same time.
2 You can perform many operations together, so it saves time.
3 Threads are independent, so it doesn’t affect other threads if an
exception occurs in a single thread.
Multitasking Multitasking is a process of executing multiple tasks
simultaneously. We use multitasking to utilize the CPU. Multitasking can
be achieved in two ways:
Process-based Multitasking (Multiprocessing)
Thread-based Multitasking (Multithreading)
Process-based Multitasking
Each process has an address in memory. In other words, each process
allocates a separate memory area.
A process is heavyweight.
Cost of communication between the process is high.
Switching from one process to another requires some time for saving
and loading registers, memory maps, updating lists, etc.
Thread-based Multitasking
Threads share the same address space.
A thread is lightweight.
Cost of communication between the thread is low.
import java . io . ∗ ;
import java . u t i l . ∗ ;
p u b l i c c l a s s GFG e x t e n d s Thread {
// i n i t i a t e d r u n method f o r Thread
p u b l i c void run ( )
{
System . o u t . p r i n t l n ( ” Thread S t a r t e d Running . . . ” ) ; }
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
GFG g1 = new GFG ( ) ;
// i n v o k i n g Thread
g1 . s t a r t ( ) ; } }
import java . io . ∗ ;
import java . u t i l . ∗ ;
p u b l i c c l a s s GFG1 i m p l e m e n t s R u n n a b l e {
// method t o s t a r t Thread
p u b l i c void run ( )
{ System . o u t . p r i n t l n ( ” Thread i s Running S u c c e s s f u l l y ” ) ;
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s )
{
GFG1 g1 = new GFG1 ( ) ;
// i n i t i a l i z i n g Thread O b j e c t
Thread t 1= new Thread ( g1 ) ;
t1 . s t a r t ( ) ; } }
import java . io . ∗ ;
i m p o r t j a v a . l a n g . Thread ;
c l a s s GFG2 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
f o r ( i n t i = 0 ; i < 5 ; i ++) {
Thread . s l e e p ( 1 0 0 0 ) ;
System . o u t . p r i n t l n ( i ) ; } }
catch ( Exception e ) {
System . o u t . p r i n t l n ( e ) ;
}}}
import java . io . ∗ ;
i m p o r t j a v a . l a n g . Thread ;
c l a s s GFG3 e x t e n d s Thread {
p u b l i c void run ( )
{ try {
f o r ( i n t i = 0 ; i < 5 ; i ++) {
Thread . s l e e p ( 1 0 0 0 ) ;
System . o u t . p r i n t l n ( i ) ; }}
catch ( Exception e ) {
System . o u t . p r i n t l n ( e ) ;
} }
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
GFG o b j = new GFG ( ) ;
obj . s t a r t ( ) ;
}}
import java . io . ∗ ;
i m p o r t j a v a . l a n g . Thread ;
c l a s s GFG5 {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
try {
f o r ( i n t i = 0 ; i < 5 ; i ++) {
Thread . s l e e p ( −100);
System . o u t . p r i n t l n ( i ) ;
}}
catch ( Exception e ) {
System . o u t . p r i n t l n ( e ) ;
}}}
import java . io . ∗ ;
c l a s s T h r e a d J o i n i n g e x t e n d s Thread
{
p u b l i c void run ( )
{
f o r ( i n t i = 0 ; i < 2 ; i ++){
try
{
Thread . s l e e p ( 1 0 0 0 ) ;
System . o u t . p r i n t l n ( ” C u r r e n t Thread : ”+ Thread . c u r r e n t T
c a t c h ( E x c e p t i o n ex ) {
System . o u t . p r i n t l n ( ” E x c e p t i o n h a s ” +” been c a u g h t ” + e
System . o u t . p r i n t l n ( i ) ; } } }
https://www.youtube.com/watch?v=Ut8YFsi2WR0