Unit 7: Multithreading Programming
Unit 7: Multithreading Programming
Unit 7
Multithreading Programming
Multithreading in Java
• Multithreading is a Java feature that allows
G
concurrent execution of two or more parts of
a program for maximum utilization of CPU.
Each part of such program is called a thread.
L
So, threads are light-weight processes within a
process.
• Multithreading in java is a process of executing
multiple threads(multiple parts of a program)
G
simultaneously.
• A thread is a lightweight sub-process, the smallest unit
of processing. Multiprocessing and multithreading,
both are used to achieve multitasking.
L
• However, we use multithreading than multiprocessing
because 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.
• Java Multithreading is mostly used in games,
animation, etc.
Advantages
1) It doesn't block the user because threads are
G
independent and you can perform multiple
operations at the same time.
2) You can perform many operations together,
L
so it saves time.
3) Threads are independent, so it doesn't affect
other threads if an exception occurs in a single
thread.
Thread in Java
L G
Lifecycle of thread
L G
Thread Priorities
• thread’s priority is used to decide when to
G
switch from one running thread to the next.
This is called a context switch.
• The rules that determine when a context
L
switch takes place are simple:
1. A thread can voluntarily relinquish control
(pending I/O)
2. A thread can be preempted by a higher-
priority thread (preemptive multitasking)
The Thread Class and the Runnable
Interface
• Java’s multithreading system is built upon the
G
Thread class, its methods, and its companion
interface, Runnable
L
Thread Class
L G
Runnable Interface
L G
L G
O/p
L G
Thread Class
L G
L G
Runnable Interface
L G
L G
Main Thread
L G
O/p
L G
Multiple Threads
L G
Multiple Threads
L G
L G
O/P
L G
isAlive () and join() methods
(1) public final boolean isAlive()
G
isAlive() method tests if the thread it is called
upon is alive or not. A thread is alive if it has
been started and not yet terminated. The
L
isAlive() method returns true if the thread
upon which it is called is still running,
otherwise it returns false.
(2) public final void join() throws
InterruptedException
G
• public final void join() - Waits indefinitely for
this thread to die.
• public final void join(long millis) - Waits at
most millis milliseconds for this thread to die.
L
A timeout of 0 means to wait forever.
• public final void join(long millis, int nanos) -
Waits at most millis milliseconds
plus nanos nanoseconds for this thread to die.
Example
L G
L G
L G
L G
Thread Priorities
• Thread priorities are used by the thread
scheduler to decide when each thread should
G
be allowed to run.
• Each thread have a priority.
• Priorities are represented by a number
between 1 and 10. In most cases, thread
L
scheduler schedules the threads according to
their priority (known as preemptive
scheduling). But it is not guaranteed because
it depends on JVM specification that which
scheduling it chooses.
3 constants
• 3 static variables defined in Thread class.
1) public static int MIN_PRIORITY: This is
G
minimum priority that a thread can have.
Value for this is 1.
2) public static int NORM_PRIORITY: This is
L
default priority of a thread if do not explicitly
define it. Value for this is 5.
3) public static int MAX_PRIORITY: This is
maximum priority of a thread. Value for this
is 10.
Get & Set method
1) public final int getPriority():
java.lang.Thread.getPriority() method returns
G
priority of given thread.
2)public final void setPriority(int newPriority):
L
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.
L G
L G
L G
Parent & Child thread priority
L G
Daemon Thread
• It provides services to user threads for
G
background supporting tasks. It has no role in
life than to serve user threads.
• Its life depends on user threads.
L
• It is a low priority thread.
Example
L G
L G
Synchronisation
• When two or more threads need access to a
G
shared resource, they need some way to
ensure that the resource will be used by only
one thread at a time. The process by which
L
this is achieved is called synchronization.
• Synchronization in java is the capability to
control the access of multiple threads to any
shared resource.
• Prevent Thread Interference & Consistency
Problem.
• Key to synchronization is the concept of the
monitor (also called a semaphore).
• A monitor is an object that is used as a mutually
G
exclusive lock, or mutex. Only one thread can own
a monitor at a given time.
• When a thread acquires a lock, it is said to have
entered the monitor.
L
• All other threads attempting to enter the locked
monitor will be suspended until the first thread
exits the monitor. These other threads are said to
be waiting for the monitor.
• A thread that owns a monitor can reenter the
same monitor if it so desires.