Multithreading allows a process to be split into multiple threads to allow parallel processing and faster execution. A thread is a sub-division of a process that can run concurrently with other threads. There are two main ways to create threads in Java: by extending the Thread class or implementing the Runnable interface. Threads have lifecycles and states like new, runnable, running, blocked, waiting, and terminated. Synchronization is used to control access to shared resources and prevent interference between threads.
2. I N T R O D U C T I O N T O T H R E A D
• Process and Thread are two basic units of the program execution.
• A process is something that is currently under execution. So, an active
program can be called a Process.
• A thread is a sub-division of a process. Thread is a single sequence
stream within a process.
• play a very significant role in the faster execution of a process.
• A process is sub-divided into sub-tasks called threads, so multiple threads
function parallel (parallel processing) and is termed as Multi-threading.
6. D E F I N I T I O N
• Multitasking allows a user to perform more than one computer task
simultaneously. Multiple tasks are also known as processes that share similar
processing resources like a CPU.
• Multiprogramming: When multiple programs execute at a time on a single
device, it is multiprogramming.
• Multitasking: When a single resource is used to process multiple tasks then it is
multitasking.
• Multithreading: It is an extended form of multitasking.
• Multiprocessing: When more than one processing unit is used by a single
computer then it is called multiprocessing.
9. 1.NEW – a newly created thread that has not yet started the execution
2.RUNNABLE – ready for execution but it's waiting for resource allocation
3.RUNNING _When the thread gets the CPU, it moves from the runnable to
the running state.
4.BLOCKED – waiting to acquire a monitor lock to enter or re-enter a
synchronized block/method
5.WAITING – waiting for some other thread to perform a particular action
without any time limit
6.TERMINATED – has completed its execution
10. M E T H O D S U S E D I N
M U LT I T H R E A D I N G
11. C R E AT I N G T H E T H R E A D S
• There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
12. C O M M O N LY U S E D M E T H O D S O F
T H R E A D C L A S S :
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.
public void join(): waits for a thread to die.
public int getPriority(): returns the priority of the thread.
public int setPriority(int priority): changes the priority 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(depricated).
public void resume(): is used to resume the suspended thread(depricated).
public void stop(): is used to stop the thread(depricated).
public boolean isDaemon(): tests if the thread is a daemon thread.
13. C O N S T R U C T O R S O F T H R E A D
C L A S S :
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)
14. T H R E A D C L A S S :
• Thread class provide constructors and methods to create and perform
operations on a thread.
• Thread class extends Object class and implements Runnable interface.
15.
16. C R E AT I N G T H E T H R E A D S
• There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
18. class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
19. 2 . I M P L E M E N T I N G R U N N A B L E
I N T E R FA C E
20. class Multi implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi m1=new Multi();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
t1.start();
}
}
21. • If we are not extending the Thread class, your class object would not be
treated as a thread object. So you need to explicitly create the Thread
class object. We are passing the object of your class that implements
Runnable so that your class run() method may execute.
22. • By implementing Runnable interface is a better way to create a
thread in java because when we create a thread by extending
Thread class, all Thread class methods are inherited while we
can perform the task with the one method (run method) only. It
results into overhead inheritance.
• Other reason is that java does not support multiple inheritance
in case of classes. So if we create a thread by extending the
Thread class, we will not be able to extend any other class.
23. • Thread class is in Java.lang package.
• run() method will contain the code for created thread.
• you can not directly call run method to start a thread. You need to call
start method to create a new thread.
• We can call run method directly. Only difference is that when start method
is called it creates a separate call stack for that thread but in case when
run method is called directly from main method it will not create a new call
stack.
24. T H R E A D P R I O R I T Y
• Each thread has a priority. Priorities are represented by a number
between 1 and 10.
• The default priority is set to 5 as excepted.
• Minimum priority is set to 1.
• Maximum priority is set to 10.
25. 3 constants defined in Thread class:
1.public static int MIN_PRIORITY
2.public static int NORM_PRIORITY
3.public static int MAX_PRIORITY
26. T H E A C C E P T E D V A L U E O F P R I O R I T Y F O R A
T H R E A D I S I N T H E R A N G E O F 1 T O 1 0 .
get and set priority of a thread in java.
1.public final int getPriority(): java.lang.Thread.getPriority() method returns priority of given
thread.
2.public final void setPriority(int newPriority): java.lang.Thread.setPriority() method
changes the priority of thread to the value newPriority. This method throws
IllegalArgumentException if value of parameter newPriority goes beyond minimum(1) and
maximum(10) limit.
• currentThread() method to get the name of the current thread.
• setName() method to make names of thread as per choice for understanding purposes.
• getName() method will be used to get the name of the thread.
27. S Y N C H R O N I Z AT I O N I N J AVA
• Synchronization in Java is the capability to control the access of multiple
threads to any shared resource.
• Java Synchronization is better option where we want to allow only one
thread to access the shared resource.
• The synchronization is mainly used to
1.To prevent thread interference.
2.To prevent consistency problem.
28. T H R E A D S Y N C H R O N I Z AT I O N
• There are two types of thread synchronization mutual exclusive and inter-
thread communication.
1.Mutual Exclusive (no two processes can exist in the critical section at any
given point of time)
2.Cooperation (Inter-thread communication in java)
29. I N T E R -T H R E A D C O M M U N I C A T I O N :
• Inter-thread communication is a process in which a thread is
paused running in its critical region and another thread is
allowed to enter (or lock) in the same critical region to be
executed. i.e. synchronized threads communicate with each
other.
30. O B J E C T C L A S S M E T H O D S A R E U S E D F O R
I N T E R - T H R E A D C O M M U N I C A T I O N P R O C E S S
• wait(): this method instructs the current thread to release the monitor held
by it and to get suspended until some other threads sends a notification from
the same monitor.
• Syntax: public void wait() throws InterruptedException.
• notify(): this method is used to send the notification to the thread that is
suspended by the wait() method.
• Syntax: public void notify().
• notifyAll(): this method is used to send the notification to all the threads
that are suspended by wait() method.
• Syntax: public void notifyAll().
31. P R O D U C E R - C O N S U M E R P R O B L E M T O
U N D E R S T A N D I N T E R - T H R E A D
C O M M U N I C A T I O N .
• The producer-consumer problem (also known as the bounded-buffer
problem) is a classic example of a multi-process synchronization problem.
The problem describes two processes, the producer and the consumer,
which share a common, fixed-size buffer used as a queue.
• The producer’s job is to generate data, put it into the buffer, and start
again.
• At the same time, the consumer is consuming the data (i.e. removing it
from the buffer), one piece at a time.
32. • Problem
To make sure that the producer won’t try to add data into the buffer if it’s
full and that the consumer won’t try to remove data from an empty buffer.
33. • Solution
The producer is to either go to sleep or discard data if the buffer is full.
The next time the consumer removes an item from the buffer, it notifies
the producer, who starts to fill the buffer again. In the same way, the
consumer can go to sleep if it finds the buffer to be empty. The next time
the producer puts data into the buffer, it wakes up the sleeping
consumer.