Contents of Lecture Threads Multithreads Threads Models Multithreads in Java Advantages Disadvantages
Contents of Lecture Threads Multithreads Threads Models Multithreads in Java Advantages Disadvantages
Threads
Multithreads
Threads models
Multithreads in java
Advantages
Disadvantages
M. Arshad
Lecturer: computer science
CUIT Peshawar
What is threads
A thread is the smallest unit of
processing that can be performed in an
OS.
A thread exists within a process.
a single process may contain
multiple threads.
A process may be divided into a number
of independent units known as threads,
A thread is a dispatchable unit of work.
Threads are light-weight processes within
a process
04/06/2020 2
As each thread has its own
independent resource for process
execution, multiple processes can
be executed parallely by
increasing number of threads.
A threads its self is not a program
but its run in program
Each thread belongs to exactly
one process and no thread can
exist outside a process
04/06/2020 3
Life Cycle of a Thread
04/06/2020 4
Following are the stages of the life cycle
New − A new thread begins its life cycle
in the new state. It remains in this state
until the program starts the thread. It is
also referred to as a born thread.
Runnable − After a newly born thread is
started, the thread becomes runnable. A
thread in this state is considered to be
executing its task.
Waiting − Sometimes, a thread
transitions to the waiting state while the
thread waits for another thread to
perform a task
04/06/2020 5
Timed Waiting − A runnable
thread can enter the timed waiting
state for a specified interval of
time. A thread in this state
transitions back to the runnable
state when that time interval
expires or when the event it is
waiting for occurs.
Terminated (Dead) − A runnable
thread enters the terminated state
when it completes its task or
otherwise terminates
04/06/2020 6
process
A process is basically a program
in execution
The execution of a process must
progress in a sequential fashion.
When a program load from
secondary memory into primary
memory its become a process,
04/06/2020 7
A process is defined as an entity
which represents the basic unit of
work to be implemented in the
system.
To put it in simple terms, we write
our computer programs in a text
file and when we execute this
program, it becomes a process
which performs all the tasks
mentioned in the program,
04/06/2020 8
Difference b/w process and threads
process threads
Process is heavy weight or Thread is light weight, taking
resource intensive. lesser resources than a
process.
One process can have multiple One threads belong to only
threads one process
04/06/2020 9
Types of Threads
04/06/2020 10
User Level Threads
User thread are implemented by
users
Its manage by user level library
i.e p.threads in C/C++ for
creating threads in a program
User level threads are typically
fast
Context switching is faster
04/06/2020 11
Ifone user level thread block
then the entire process will be
block
WHY????
◦ kernel is not aware and it manage
by the user level libraries
Kernel
Multiple threads
04/06/2020 12
User level thread
04/06/2020 13
Kernel level thread
In this case, thread management
is done by the Kernel,
Kernel threads are supported
directly by the operating system,
The Kernel maintains context
information for the process as a
whole and for individuals threads
within the process.
04/06/2020 14
Ifone kernel level thread is
blocked the entire process will
not be blocked
WHY???
◦ Maintain and control by the kernel
Take more time in context switching
system call is involved,
04/06/2020 15
Multithreading
04/06/2020 18
example
Word processor
i.e
When you writing in word
documents
so there are multiple threads
counting numbers( words)
spell check
page counting multiple
threads
grammar check04/06/2020 19
Multithreading Models
04/06/2020 20
Multithreading models are three
types,
Many to many relationship.
Many to one relationship.
One to one relationship.
04/06/2020 21
One to One Model
The one to one model creates a
separate kernel thread to handle
each and every user thread.
Most implementations of this
model place a limit on how many
threads can be created.
Linux and Windows from 95 to XP
implement the one-to-one model
for threads.
04/06/2020 22
It also allows another thread to run
when a thread makes a blocking
system call,
It supports multiple threads to execute
in parallel on microprocessors.
Disadvantage of this model is that
creating user thread requires the
corresponding Kernel thread.
04/06/2020 23
04/06/2020 24
04/06/2020 25
Many to One Model
04/06/2020 27
Many to one model
04/06/2020 28
OR
04/06/2020 29
Many to Many Model
04/06/2020 31
A diagram that demonstrates the
many to many model
04/06/2020 32
The diagram shows the many-to-
many threading model where 6
user level threads are
multiplexing with 6 kernel level
threads,
In this model, developers can
create as many user threads as
necessary and the corresponding
Kernel threads can run in parallel
on a multiprocessor machine.
04/06/2020 33
This model provides the best
accuracy on concurrency and
when a thread performs a
blocking system call, the kernel
can schedule another thread for
execution.
04/06/2020 34
Advantages of Multithreaded Programming
04/06/2020 36
Disadvantages of Multithreaded
Programming
04/06/2020 38
Extra lecture
Java - Multithreading
Java is a multi-threaded
programming language which
means we can develop multi-
threaded program using Java,
A multi-threaded program
contains two or more parts that
can run concurrently and each
part can handle a different task
at the same time making optimal
use of the available resources
specially when your computer
04/06/2020 39
Multi-threading enables you to
write in a way where multiple
activities can proceed
concurrently in the same
program.
04/06/2020 40
Create a Thread by Implementing a Runnable Interface
Where, threadObj isan instance of a
class that implements
the Runnable interface
and threadName is the name given
to the new thread.
Step 3
Once a Thread object is created, you can
start it by calling start() method
which executes a call to run( ) method
04/06/2020 43
Following is a simple syntax of
start() method
void start();
04/06/2020 44
Example
Here is an example that creates a new thread and starts running it −
04/06/2020 45
}
catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting."); }
public void start () {
System.out.println("Starting " + threadName );
if (t == null) {
t = new Thread (this, threadName);
t.start ();
}
}
} public class TestThread {
public static void main(String args[]) {
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start();
RunnableDemo R2 = new RunnableDemo( "Thread-2");
R2.start();
}
}
04/06/2020 46
output
Creating Thread-1 Starting
Thread-1 Creating Thread-2
Starting Thread-2 Running
Thread-1 Thread: Thread-1, 4
Running Thread-2 Thread:
Thread-2, 4 Thread: Thread-1, 3
Thread: Thread-2, 3 Thread:
Thread-1, 2 Thread: Thread-2, 2
Thread: Thread-1, 1 Thread:
Thread-2, 1 Thread Thread-1
exiting. Thread Thread-2 exiting.
04/06/2020 47
04/06/2020 48