0% found this document useful (0 votes)
3 views4 pages

Multi Threading

The document discusses multithreading, explaining its definition and advantages, such as time savings and non-blocking user experience. It outlines the lifecycle of threads, deadlock conditions, and race conditions, along with two methods for implementing threads: extending the Thread class and implementing the Runnable interface. Additionally, it contrasts processes and threads, highlighting their differences in terms of weight and communication ease.

Uploaded by

srini221005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Multi Threading

The document discusses multithreading, explaining its definition and advantages, such as time savings and non-blocking user experience. It outlines the lifecycle of threads, deadlock conditions, and race conditions, along with two methods for implementing threads: extending the Thread class and implementing the Runnable interface. Additionally, it contrasts processes and threads, highlighting their differences in terms of weight and communication ease.

Uploaded by

srini221005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Multithreading – many + threads

Part1 – printing the numbers in ascending order 1-10


Part2 – printing the numbers in descending order 10-1
Both parts are in same program.
In normal scenario part1 starts running first, after part1 completes
patr2 starts running.(like a sequential fashion).
Thread – part of the program running inside memory.
Part1 – thread1
Part2 – thread2
In multithreading both the parts/threads runs together.
The process of running multiple threads/parts simultaneously or
parallelly or together at same time is called as multithreading.
Advantages :
1. Saves/reduce time.
2. It does not block the user or user do not need to wait since it can
run at the same time.
3. Threads are independent.
Process(Program) Thread(part of the program/process)
Thread1 2344
Process1
2344 Thread2 2344
4567 6345 Thread3
Process1
Process2 Process3
 2344
Process Thread
Program running inside the Part of the program(process –
memory is called as process. Each subprocess). It will have the same
process will have a address address assigned to that of
assigned to it and each address is process.
unique.
It is heavyweight. It is lightweight.
Communication is difficult. Communication is easy.

Lifecycle of thread –
[Link]
Deadlock condition – p – process/program, v-variables/resources

P1 P2 P3

waiting waiting waiting

V3
V1 V2

Deadlock is a situation where a set of processes is blocked/waited


because each process is holding a resource/variable and waiting for
another resource which is there with another process.
Race condition - When two or more process try to access and update
the same resource or data.
Ajay Kumar
Update customer
data

?????

P1 P2

Ajay K Ajay R
Two ways of implementing of Threads:
1. Extends Thread class
a. Create a class/classes and extends the inbuilt Thread class.
b. Start the thread/s by writing constructor and calling start().
c. Override the run() method present in Thread parent class. And
implement the logic inside it.
d. Make threads to sleep for some ms and add try catch block.
e. Lastly call the threads class objects in main class.

2. Implements Runnable Interface


a. Create a class/classes and implements the inbuilt Runnable
interface.
b. Create a constructor, create a object of Thread class and start the
thread using start().

c. Override the run() method present in Runnable parent interface.


And implement the logic inside it.
d. Make threads to sleep for some ms and add try catch block.
e. Lastly call the threads class objects in main class.

You might also like