Multi Threading
Multi Threading
Unit-12
Concurrency /
Multithreading
Multithreading
Life Cycle of Thread
Creating a Thread
Using Thread Class
Using Runnable Interface
Join
Synchronized
What is Multithreading?
Multithreading in Java is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing.
Multiprocessing and multithreading, both are used to achieve multitasking.
Threads use a shared memory area. They don't allocate separate memory area so saves
memory, and context-switching between the threads takes less time than process.
A thread goes through various stages in its life cycle. For example, a thread is born, started,
runs, and then dies.
Lets see the life cycle of the thread.
Th
l runnable
un gna ll Waiting: Sometimes a thread transitions to the waiting state
rea Tas
si alA
dc k
n while the thread waits for another thread to perform a task. A
si g
om
Interval
expires
ait thread transitions back to the runnable state only when
await
ple
sleep
aw ck
tes
lo another thread signals waiting thread to continue.
Timed waiting: A runnable thread can enter the timed
timed waiting state for a specified interval of time. A thread in this
waiting terminated
waiting state transitions back to the runnable state when that time
interval expires or when the event it is waiting for occurs.
Terminated: A runnable thread enters the terminated state
when it completes its task or otherwise terminates.