0% found this document useful (0 votes)
9 views37 pages

Java Module5 v0.1

java notes

Uploaded by

Vinayak Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views37 pages

Java Module5 v0.1

java notes

Uploaded by

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

JAVA-Module-5

Threads
CHANNAVEERAYA WM
Assistant Professor, Department of CS&E
The National Institute of Engineering, Mysuru

linkedin.com/in/channaveeraya-wujjini-matada-9323618
(Ex HCL-Perot Systems, Ex Siemens, Ex CSR(Qualcomm), Ex Dell, Ex Ericsson, Ex Ambient
Scientific Inc )

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 2


18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 4
Multithreaded Programming-
• A thread is a path of execution through the software that has its own
call-stack and CPU state.
• Multithreaded Programming is implementing software so that two or
more activities can be performed in parallel within the same
application.
• This is accomplished by having each activity performed by its own
thread.
• Threads runs within the context of a process which defines address-
space within which code and data exist and threads execute.
• Process is the container for threads.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 5


Multithreaded Programming-
• A thread is a path of execution through the software that has its own call-stack and CPU
state.
• Multithreaded Programming is implementing software so that two or more activities can be
performed in parallel within the same application.
• This is accomplished by having each activity performed by its own thread.
• Threads runs within the context of a process which defines address-space within which code
and data exist and threads execute.
• Process is the container for threads.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 6


Advantages of Multithreaded Programming
• Faster Processing
• Simplified design
• Consider audio playback program. This can be designed to use three threads.
• First thread for reading compressed audio from the disk file.
• Second thread for de-compressing the audio stream.
• Third thread for playing back the de-compressed audio data to the speakers.
• This design allows the file-reading thread (First thread) which will spend large portion of its time
blocked waiting for disk I/O operations to complete.
• Increased Robustness
• By using multiple threads within a program, it is possible to increase the robustness of
an application by isolating critical sub-systems into their own thread of control.
• Increased Responsiveness to the user.
• Multithreading can enhance responsiveness in applications that involve user interaction.
By separating time-consuming tasks from the main thread, the user interface can remain
responsive and not freeze or become unresponsive.
• Better usage of the CPU:
• Multithreading can improve the performance and efficiency of a program by utilizing the
available CPU resources more effectively
18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 7
Threads in Java – Main Thread
• When a Java program starts up, one thread begins running immediately. This is usually
called the main thread of your program, because it is the one that is executed when your
program begins. The main thread is important for two reasons
• It is the thread from which other “child” threads will be spawned.
• Often, it must be the last thread to finish execution because it performs various shutdown actions.
• Although the main thread is created automatically when your program is started, it can be
controlled through a Thread object.
• To do so, you must obtain a reference to it by calling the method currentThread( ), which is a
public static member of Thread.
• Its general form is shown here: static Thread currentThread( )
• This method returns a reference to the thread in which it is called. Once you have a reference
to the main thread, you can control it just like any other thread

Note: Refer page 251 of “The Complete Reference” for more theory for the above,

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 8


Threads in Java – Main Thread
• Here is the example
involving main thread and
controlling it.
• In this program, a reference
to the current thread (the
main thread, in this case) is
obtained by calling
currentThread(), and this
reference is stored in the
local variable t.
• The program displays
information about the
thread.
• The program then calls
setName( ) to change the
internal name of the thread

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 9


Threads in Java
• Java threads can be created by the following two ways.
• By extending the thread class

• By implementing a Runnable interface

Note: Refer page 253 of “The Complete Reference” for more theory for the above,
18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 10
Threads in Java - Extending Thread Class
• Define a subclass that extends from the superclass Thread.
• In the subclass, override the run() method to specify the thread's operations,
(and provide other implementations such as constructors, variables and
methods).
• A client class creates an instance of this new class. This instance is called a
Runnable object (because Thread class itself implements Runnable interface).
• The client class invokes the start() method of the Runnable object. The result
is two thread running concurrently – the current thread continue after invoking
the start(), and a new thread that executes run() method of the Runnable
object.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 11


Threads in Java - Extending Thread Class

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 12


Threads in Java - extending the thread class

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 13


Threads in Java - Implementing Runnable Interface
• Define a class that implements the
Runnable interface.
• In the class, provide implementation to
the abstract method run() to specify
the thread's operations, (and provide
other implementations such as
constructors, variables and methods).
• A client class creates an instance of
this new class. The instance is called a
Runnable object.
• The client class then constructs a
new Thread object with the
Runnable object as argument to the
constructor, and invokes the start()
method. The start() called back the
run() in the Runnable object (instead
of the Thread class).

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 14


Threads in Java - Implementing Runnable Interface
• Again, an inner
class (named or
anonymous) is
often used for
readability and to
provide access to
the private
variables and
methods of the
outer class.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 15


Threads in Java - Implementing Runnable Interface

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 16


Threads in Java - Life-Cycle of Thread

https://www3.ntu.edu.sg/home/ehchua/programming/java/j5e_multithreading.html

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 17


Threads in Java - Example-1 Real Usecase (using Thread Class):
Multithreaded File Download

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 18


Threads in Java - Example-1 Real Usecase (using Thread Class):
Multithreaded File Download:

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 19


Threads in Java - Example-1 Real Usecase using Runnable interface:
Parallel task Execution

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 20


Threads in Java - Example-1 Real Usecase using Runnable interface
Parallel task Execution :

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 21


Commonly used Constructors of Thread class:
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 22


Commonly used methods of Thread class:

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 23


Commonly used methods of Thread class:

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 24


Synchronization in Java Threads:
• Synchronization in Java is the capability to control the access of multiple threads
to any shared resource.
• Synchronization is used when we want to allow only one thread to access the
shared resource.
Why use Synchronization?
The synchronization is mainly used to
• To prevent thread interference.
• To prevent consistency problem.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 25


Thread Synchronization:
There are two types of thread synchronization mutual exclusive and inter-thread
communication.
• Mutual Exclusive
• Synchronized method.
• Synchronized block.
• Cooperation (Inter-thread communication in java)

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 26


Thread Synchronization: Synchronized method

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 27


Thread Synchronization: Synchronized method

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 28


Thread Synchronization: Synchronized method

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 29


Thread Synchronization: Synchronized method
• Without Synchronization: OUTPUT:

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 30


Thread Synchronization: Synchronized method
• With Synchronization: OUTPUT:

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 31


Thread Synchronization: Cooperation (Inter-thread communication in java)
• Interthread communication in java is implemented using the following methods

• The wait() and notify() methods provide a way for a shared object to pause a
thread when it becomes unavailable to that thread and to allow the thread to
continue when available.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 32


Thread Synchronization: Consumer-Producer Problem
• In computing or java, the consumer-producer
problem is multiple threads trying to access
single resources with synchronized block.
• Consumer-producer problem is when consumer
and producer accessing buffer at the same
time.
• Problem occurs in the following conditions:
• Consumer:- When Consumer trying to access empty
buffer.
• Producer:- When the producer trying to add data
more than the buffer size.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 33


Thread Synchronization: Consumer-Producer Problem
• In computing or java, the consumer-producer
problem is multiple threads trying to access
single resources with synchronized block.
• Consumer-producer problem is when consumer
and producer accessing buffer at the same
time.
• Problem occurs in the following conditions:
• Consumer:- When Consumer trying to access empty
buffer.
• Producer:- When the producer trying to add data
more than the buffer size.

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 34


Thread Synchronization: Consumer-Producer Problem

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 35


Thread Synchronization: Consumer-Producer Problem

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 36


Thread Synchronization: Consumer-Producer Problem

18-02-2024 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 37

You might also like