10 Multithreading
10 Multithreading
1
Faculty Name: Programe Name: B.Tech
List of Topics to be covered
Different types of Execution Techniques
Thread
Multithreading
Different types of multitasking thread-based and
process-based multitasking
Benefits of Multithreading
Life Cycle of Thread
Multithreaded Server: For Serving Multiple Clients
Concurrently
Creating threads in Java
2
Different types of Execution Techniques
Multitasking: Ability to execute more than one task at the same
time is known as multitasking.
4
Multiprocessing in Java
Multiprocessing in Java is purely based on the number of processors available
on the host computer. Every process initiated by the user is sent to the CPU
(processor). It loads the registers on the CPU with the data related to the
assigned process.
To perform multiprocessing in Java, the user needs one processor. Therefore,
when the user requests the simultaneous execution of the second process, the
alternate CPU core gets triggered and executes the process.
5
Multithreading in Java
Multithreading in Java is a similar approach to multiprocessing. However,
there are some fundamental differences between the two. Instead of a
physical processor, multithreading involves virtual and independent
threads.
It assigns each process with one or more threads based on their
complexity. Each thread is virtual and independent of the other. This
makes process execution much safer. If a thread or two are terminated
during an unexpected situation, the process execution will not halt.
6
Thread
Thread: single sequential flow of control within a program
7
Benefits of Multithreading
Enables programmers to do multiple things at one time
Programmers can divide a long program into threads and execute
them in parallel which eventually increases the speed of the program
execution
Improved performance and concurrency
Simultaneous access to multiple applications
Different processes do not share memory space.
A thread can execute concurrently with other threads within a
single process.
All threads managed by the JVM share memory space and can
communicate with each other.
8
Way of Execution of multiple Threads
Threads Concept
Thread 1
Thread 2
on multiple CPUs
Multiple Thread 1
Thread 2
Thread 3
threads
sharing a
single CPU
9
Lifecycle of a Thread
in Java
Thread give up its control on its own and it can happen in the following situations
A thread gets suspended using suspend() method which can only be revived
with resume() method
A thread is made to wait for some event to occur using wait () method. In this
case a thread can be scheduled to run again using notify () method.
11
Different Stages of a Thread
A thread can be in any of the five following states
Newborn State: When a thread object is created a new thread is born and said to be
in Newborn state.
Runnable State: If a thread is in this state it means that the thread is ready for
execution and waiting for the availability of the processor. If all threads in queue
are of same priority then they are given time slots for execution in round robin
fashion
Running State: It means that the processor has given its time to the thread for
execution.
Blocked State: If a thread is prevented from entering into runnable state and
subsequently running state, then a thread is said to be in Blocked state.
Dead State: A runnable thread enters the Dead or terminated state when it completes
its task or otherwise terminates.
12
A single threaded program
class ABC
{
….
public static void main(..) begin
{
…. body
….
....
}
end
}
13
Multithreaded Program
A Multithreaded Program
Main Thread
start
start
start
16
Thread Creation
There are two ways to create thread
17
1st method: Extending Thread class
Create a class by extending Thread class and override
run() method:
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
Create a thread:
MyThread thr1 = new MyThread();
20
Commonly used methods of Thread class
public void run(): is used to perform action 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.
24
Starting a Thread
start() method of Thread class is used to start a newly created
thread. It performs following tasks:
When the thread gets a chance to execute, its target run() method will
run.
25
Method 1: Thread creation by extending Thread class
class MultithreadingDemo extends Thread
{
public void run(){
System.out.println("My thread is in running state.");
}
public static void main(String args[]){
MultithreadingDemo obj1=new MultithreadingDemo();
MultithreadingDemo obj2=new MultithreadingDemo();
obj1.start();
obj2.start();
} Output:
} My thread is in running state.
26 My thread is in running state.
Method 2: Thread creation by implementing Runnable Interface
//A Simple Example If you are not extending the
class MultithreadingDemo implements Runnable Thread class,your class object
would not be treated as a
{ thread object.
public void run(){
System.out.println("My thread is in running state."); So you need to explicitly create
Thread class object.
}
We are passing the object of
public static void main(String args[]) your class that implements
{ Runnable so that your class
MultithreadingDemo obj=new MultithreadingDemo(); run() method may execute.
Thread tobj =new Thread(obj);
Thread tobj =new Thread(new MultithreadingDemo()); //Another way to create
tobj.start();
}
}
28
Thread Priority in Java Multi-Threading
When we talk about thread we use terms like concurrent threads or
threads executing concurrently.
But in reality threads also run one at a time (at least in a single CPU
system), threads are given CPU cycle in a time shared manner to
simulate concurrency.
29
Java Thread Priority
They are as following:
39
References:
https://www.geeksforgeeks.org/
https://www.javatpoint.com/exception-handling-in-java
https://www.tutorialspoint.com/java/java_exceptions.htm
The complete reference, eleventh edition, available at:
https://gfgc.kar.nic.in/sirmv-science/GenericDocHandler/1
38-a2973dc6-c024-4d81-be6d-5c3344f232ce.pdf
40
Thank you
41
Multiprogramming vs Multitasking
42
Multithreading
Multithreading is a conceptual programming concept where a program (process)
is divided into two or more subprograms (process), which can be implemented at
the same time in parallel.
A multithreaded program contains two or more parts that can run concurrently.
Each part of such a program is called a thread, and each thread defines a separate
path of execution.
A process consists of the memory space allocated by the operating system that can
contain one or more threads.
43
Multitasking
There are two distinct types of Multitasking i.e.
Processor-Based and
Thread-Based multitasking.
44
Comparison
As both are types of multitasking there is very basic difference between the
two.
Process-Based multitasking is a feature that allows your computer to run
two or more programs concurrently. For example you can listen to music
and at the same time chat with your friends on Facebook using browser.
46
Process & Multithreading
47
Multitasking thread requires less overhead
than multitasking processor
A multitasking thread requires less overhead than multitasking processor because
of the following reasons:
Processes require their own separate address space where threads share the
address space
48
49
50