Unit-5 Multithreading
Unit-5 Multithreading
SEM – III
0301301 - CORE JAVA
●
It doesn't block the user because threads are independent
and you can perform multiple operations at same time.
●
You can perform many operations together so it saves
time.
●
Threads are independent so it doesn't affect other threads
if exception occur 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 by two ways:
1) Process-based Multitasking (Multiprocessing)
2) Thread-based Multitasking (Multithreading)
Multitasking
●
A thread is a lightweight sub process, a smallest unit
of processing.
●
It is a separate path of execution.
●
Threads are independent, if there occurs exception in one
thread, it doesn't affect other threads.
●
It shares a common memory area.
●
At a time one thread is executed only.
What is Thread in java
Life cycle of a Thread (Thread States)
●
A thread can be in one of the five states.
●
The life cycle of the thread in java is controlled by JVM.
●
The java thread states are as follows:
– New
– Runnable
– Running
– Non-Runnable (Blocked)
– Terminated
Life cycle of a Thread
Life cycle of a Thread
Life cycle of a Thread
●
There are two ways to create a thread:
●
By extending Thread class
●
By implementing Runnable interface.
How to create thread
●
Thread class:
●
Thread class provide constructors and methods to create
and perform operations on a thread.
●
Thread class extends Object class and implements
Runnable interface.
●
Commonly used Constructors of Thread class:
Thread()
Thread(String name)
Thread(Runnable r)
Thread(Runnable r,String name)
Commonly used methods of Thread class:
●
public void run()
is used to perform action for a thread (entry point for a
thread).
●
public void start()
starts the execution of the thread. JVM calls the run()
method on the thread.
●
public void sleep(long miliseconds)
Causes the currently executing thread to sleep (temporarily
cease execution) for the specified number of milliseconds.
●
public void join(long miliseconds)
waits for a thread to die for the specified miliseconds.
Commonly used methods of Thread class:
●
public int getPriority()
returns the priority of the thread.
●
public int setPriority(int priority)
changes the priority of the thread.
●
public String getName()
returns the name of the thread.
●
public void setName(String name)
changes the name of the thread.
●
public Thread currentThread()
returns the reference of currently executing thread.
●
public int getId()
returns the id of the thread.
Commonly used methods of Thread class:
●
public Thread.State getState()
returns the state of the thread.
●
public boolean isAlive()
tests if the thread is alive.
●
public void yield()
causes the currently executing thread object to temporarily pause and allow
other threads to execute.
●
public void suspend()
is used to suspend the thread.
●
public void resume()
is used to resume the suspended thread.
●
public void stop()
is used to stop the thread.
Runnable interface:
●
The Runnable interface should be implemented by any
class whose instances are intended to be executed by a
thread.
●
Runnable interface have only one method named run().
public void run(): is used to perform action for a thread.
●
The most common use case of the Runnable interface is
when we want only to override the run method.
●
When a thread is started by the object of any class which
is implementing Runnable, then it invokes the run method
in the separately executing thread.
Runnable interface: