0% found this document useful (0 votes)
130 views

Contents of Lecture Threads Multithreads Threads Models Multithreads in Java Advantages Disadvantages

The document discusses threads, multithreading, and threading models. It defines a thread as the smallest unit of processing that exists within a process and can allow multiple processes to run concurrently. The life cycle of a thread and differences between processes and threads are explained. User-level and kernel-level threads are compared. Multithreading allows multiple threads to exist within a process and share resources. The three multithreading models - one-to-one, many-to-one, and many-to-many - are outlined. Advantages of multithreading like resource sharing and disadvantages like complexity are noted.

Uploaded by

Atif basir khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Contents of Lecture Threads Multithreads Threads Models Multithreads in Java Advantages Disadvantages

The document discusses threads, multithreading, and threading models. It defines a thread as the smallest unit of processing that exists within a process and can allow multiple processes to run concurrently. The life cycle of a thread and differences between processes and threads are explained. User-level and kernel-level threads are compared. Multithreading allows multiple threads to exist within a process and share resources. The three multithreading models - one-to-one, many-to-one, and many-to-many - are outlined. Advantages of multithreading like resource sharing and disadvantages like complexity are noted.

Uploaded by

Atif basir khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Contents of lecture

 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

A thread goes through various


stages in its life cycle. For
example, a thread is born,
started, runs, and then dies.

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

Multiple processes without Multiple threaded processes


using threads use more use fewer resources
resources.
Individual processes are Threads are parts of a process
independent of each other and so are dependent

Processes require more time Threads require less time for


for creation. creation.

04/06/2020 9
Types of Threads

Threadsare implemented in following


two ways

User Level Threads


kernel Level 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

Multithreading is a type of execution


model that allows multiple threads
to exist within the context of a
process such that they execute
independently,
but share their process resources,
A thread maintains a list of
information relevant to its execution
including the priority schedule,
exception handlers,
04/06/2020 16
A set of CPU registers, and stack
state in the address space of its
hosting process.
Multithreading is the ability of a 
program or an operating system 
process to manage its use
 By more than one user at a time
and to even manage multiple
requests by the same user without
having to have multiple copies of the
programm running in the computer,
04/06/2020 17
Multithreading allows the
execution of multiple parts of a
program at the same time.
These parts are known as threads
and are lightweight processes
available within the process.
So multithreading leads to
maximum utilization of the CPU
by multitasking.

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

Some operating system provide a


combined user level thread and
Kernel level thread facility,

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

Many-to-one model maps many


user level threads to one Kernel-
level thread,
Thread management is done in
user space by the thread library.
When thread makes a blocking
system call, the entire process
will be blocked.
Only one thread can access the
Kernel at a time, 
04/06/2020 26
Adisadvantage of the many to
one model is that a thread
blocking system call blocks the
entire process.

04/06/2020 27
Many to one model

04/06/2020 28
OR

04/06/2020 29
Many to Many Model

The many-to-many model


multiplexes any number of user
threads onto an equal or smaller
number of kernel threads.
combining the best features of
the one-to-one and many-to-one
models.
Users can create any number of
the threads.
04/06/2020 30
Blocking the kernel system calls
does not block the entire process.
There can be as many user
threads as required and their
corresponding kernel threads can
run in parallel on a
multiprocessor.

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

All the threads of a process share its


resources such as memory, data, files
etc. A single application can have
different threads within the same
address space using resource sharing.
it is more economical to use threads
as they share the process resources.
Comparatively, it is more expensive
and time consuming to create
processes as they require more
memory and resources.
04/06/2020 35
Program responsiveness allows a
program to run even if part of it is
blocked using multithreading,
In a multiprocessor architecture,
each thread can run on a
different processor in parallel
using multithreading. This
increases concurrency of the
system,

04/06/2020 36
Disadvantages of Multithreaded
Programming

Some of the disadvantages of


multithreaded processes are
given as follows:
Multithreaded processes are
quite complicated. Coding for
these can only be handled by
expert programmers,
It is difficult to handle
concurrency in multithreaded
processes
04/06/2020 37
This may lead to complications
and future problems.
Identification and correction of
errors is much more difficult in
multithreaded processes as
compared to single threaded
processes.

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

Ifyour class is intended to be


executed as a thread then you can
achieve this by implementing
a Runnable interface.
Step 1
As a first step, you need to
implement a run() method provided
by a Runnable interface.
This method provides an entry point
for the thread ,
04/06/2020 41
and you will put your complete
logic inside this method.
 Following is a simple syntax of
the run() method
public void run( )
Step 2
As a second step, you will
instantiate a Thread object using
the following constructor
04/06/2020 42
 Thread(Runnable threadObj, String
threadName);

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 −

class RunnableDemo implements Runnable {


private Thread t;
private String threadName;
RunnableDemo( String name) {
threadName = name;
System.out.println("Creating " + threadName );
}
public void run() {
System.out.println("Running " + threadName );
try { for(int i = 4; i > 0; i--) {
System.out.println("Thread: " + threadName + ", " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}

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

You might also like