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

Lecture 16 - Multithreading

This document discusses multithreading in Java. It explains that multithreading is used to maximize CPU utilization by allowing other threads to execute when one thread is waiting for I/O. It describes how to create threads by extending the Thread class or implementing the Runnable interface. It outlines the lifecycle of a thread and types of thread constructors. It also discusses thread priorities, introducing wait times using Thread.sleep(), and forcing one thread to wait for another using join().

Uploaded by

Priyansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Lecture 16 - Multithreading

This document discusses multithreading in Java. It explains that multithreading is used to maximize CPU utilization by allowing other threads to execute when one thread is waiting for I/O. It describes how to create threads by extending the Thread class or implementing the Runnable interface. It outlines the lifecycle of a thread and types of thread constructors. It also discusses thread priorities, introducing wait times using Thread.sleep(), and forcing one thread to wait for another using join().

Uploaded by

Priyansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Advanced Programming

CSE 201
Lecture 16: Multithreading
Multithreading

● Used to maximize the CPU utilization.


● We don't want our CPU to be in a free state; for example, Func1() is running and
demands any input/output process. The CPU unnecessarily wait until Func1()
completes its input/output operation.
● While Func1() completes its I/O operation, the CPU is free and not executing any
thread. The efficiency of the CPU is decreased in the absence of multithreading.
● In the case of multithreading, if a thread demands any I/O operation, then the CPU will
let the thread perform its I/O operation and will start the execution of a new thread. In
this case, two threads are executing at the same time. If there is a waiting time in the
new thread, the original thread can be reactivated.
Creating a Threading

There are two ways to create a thread in java


1. By extending Thread class
2. By implementing Runnable interface
By Extending Thread Class
By Implementing
Runnable Interface
Lifecycle of a thread
1. New - Instance of a thread is created, which is not yet started by invoking
start(). In this state, the thread is also known as the born thread.
2. Runnable - After invocation of start()& before it is selected to be run by the
scheduler.
3. Running - After the thread scheduler has selected it.
4. Non-runnable - thread alive, not eligible to run.
5. Terminated - run() method has exited.
Types of constructors
1. Thread ( )
2. Thread ( string )
3. Thread ( Runnable r )
4. Thread ( Runnable r, String name )
In a Multithreading environment, all the threads which are ready and waiting to be
executed are present in the Ready queue. The thread scheduler is responsible for
assigning the processor to a thread. But the question is on what basis the thread scheduler
decides that a particular thread will run before other threads. Here comes the concept of
priority in the picture.

❖ Every single thread created in Java has some priority associated with it.
❖ The programmer can explicitly assign the priority to the thread. Otherwise, JVM
automatically assigns priority while creating the thread.
❖ In Java, we can specify the priority of each thread relative to other threads. Those
threads having higher priorities get greater access to the available resources than
lower priorities threads.
❖ Thread scheduler will use priorities while allocating processor.
❖ The valid range of thread priorities is 1 to 10 (but not 0 to 10), where 1 is the
least priority, and 10 is the higher priority.
❖ If there is more than one thread of the same priority in the queue, then the
thread scheduler picks any one of them to execute.
❖ The following static final integer constants are defined in the Thread class:
● MIN_PRIORITY: Minimum priority that a thread can have. Value is 1.
● NORM_PRIORITY: This is the default priority automatically assigned by JVM to
a thread if a programmer does not explicitly set the priority of that thread. Value
is 5.
● MAX_PRIORITY: Maximum priority that a thread can have. Value is 10.
You can set
priority using
any number
between 1
and 10, not
just these
constants.
Introducing wait time
though Thread.sleep().

T2 started right in the


first iteration in t1
because of the wait
time introduced.
Here, 1000 means
1000 ms, i.e. 1 sec.
join()
Forcing current thread to
wait until a particular thread
completes its job using join
function.

In this case the main thread


is the current thread and t1
is the particular thread.

Therefore, despite long


waits in t1, t2 couldn’t start,
as main thread was waiting
for t1 to finish before
t2.start() statement can be
reached.

You might also like