SlideShare a Scribd company logo
Threads
In JAVA
-M Vishnuvardhan,
Dept. of Computer Science,
SSBN Degree College, ATP
SSBN Degree College, ATP M Vishnuvardhan
Introduction
The ability of an Operating Systems to perform multiple jobs
concurrently is called as Multi-Tasking. Similarly in
programming, we divide the program in to multiple units and
execute them concurrently is called as Multi-Threading. Multi-
Threading improves the performance of the Program and makes
the programs to execute in a faster way
Java Supports Multi-Threading.
SSBN Degree College, ATP M Vishnuvardhan
Thread – Defined
A Thread is a piece of control which performs a job.
A thread is considered as light weight process since it
doesn't put burden on the CPU by demanding new
resources when a new thread is created instead it shares
the resources of its parent thread.
But a process always burden on the CPU by demanding
new resources when a new Process is created.
In order to deal with the threads java defined Thread
class in java.lang package
SSBN Degree College, ATP M Vishnuvardhan
Creation of Threads
In java Threads can be created in two ways
 By extending Thread class
 By implementing Runnable interface
class NumThread extends Thread
{
====
}
class NumThread implements Runnable
{
====
}
SSBN Degree College, ATP M Vishnuvardhan
Creation of Threads
Steps for creating thread by extending Thread class
1.Create a class by extending Thread class
2.Override run() method defined in Thread class. The run() is
considered as heart of the Thread since it holds the job of the Thread
3.Create an object for the newly created thread
4.Start the thread using start() method present in the Thread class
class NumThread extends Thread
{
public void run()
{
//job of the thread
}
}
NumThread T1=new NumThread();
T1.start();
SSBN Degree College, ATP M Vishnuvardhan
Creation of Threads
Steps for creating thread by implementing Runnable interface
1.Create a class by implementing Runnable interface
2.Override run() method declared in Runnable interface.
3.Create an object for the newly created class
4.Create a Thread class object by passing the reference of new
created object
5.Now start the thread using start() method
class NumThread implements Runnable
{
public void run()
{
//job of the thread
}
}
NumThread T1=new NumThread();
T1.start(); // gives error
Thread T2=new Thread(T1);
T2.start();
SSBN Degree College, ATP M Vishnuvardhan
Life Cycle of the Thread
start()
suspend()
sleep()
wait()
resume()
notify()
RunnableRunning
New Born
Blocked
Dead
yeild()
stop()
stop()
stop()
SSBN Degree College, ATP M Vishnuvardhan
Thread Methods
 void start(): used to start a Thread
 void stop(): used to stop a Thread
 void run() : used to specify the job of the Thread
 void sleep( int milliseconds): used to suspend the Thread for
a definite time
 void suspend(): used to suspend a Thread for a indefinite
time until the Thread uses resume() method
 void resume(): used to resume a suspended thread
 void wait(): used to suspend a thread until other Thread
notifies it using notify()
 void notify(): used to notify a waiting Thread.
SSBN Degree College, ATP M Vishnuvardhan
Thread Exceptions
 IllegalThreadStateException: Occurs when a Thread is moved
to invalid state of thread.
 InterruptedException: Occurs when a Thread is interrupted.
Generally sleep() generates this exception
SSBN Degree College, ATP M Vishnuvardhan
Thread Priority
Priority is an integer number which is associated with each
thread. Which specifies the priority of the Threads.
In general threads contains three priorities. These are defined
as constants in Thread class.
MIN_PRIRORITY --- 1
NORM_PRIRORITY --- 5
MAX_PRIRORITY --- 10
Priority for threads can given using following methods
int getPriority(): gets the priority of the thread
void setPriority(int priority): sets the priority for the thread
Eg: T1.setPriority(Thread.MAX_PRIORITY);
SSBN Degree College, ATP M Vishnuvardhan
Need of Synchronization
Account
accountNo
Name
Balance
getBalance()
Deposit()
withDraw()
Thread A Thread B
deposit(5000) withDraw(3000)
SSBN Degree College, ATP M Vishnuvardhan
Synchronization
It is common that two or more threads do the same job. This
doesn't cause any problem as long as the job doesn’t contain
critical section.
A section is said to be critical section when the statements
share common data.
When multiple threads work on critical section some times they
result in Data Integrity failure.
Synchronization is a technique of allowing only one thread in to
critical section at a time.
It can be applied in two levels
Method level Synchronization
Block level Synchronization
SSBN Degree College, ATP M Vishnuvardhan
Method level Synchronization
If all the statements inside a method are critical section
then we apply synchronization at method level. i.e, only one
thread is allowed in that method until that thread completes its
job no other thread is allowed to enter in to the method.
Syntax: synchronized returnType methodName ( <<parms>>)
{
======
}
Eg: synchronized double getBalance()
{
return balance;
}
SSBN Degree College, ATP M Vishnuvardhan
Block level Synchronization
If only few statements inside a method are critical section but
not all the statements then we apply synchronization at block level. i.e,
only one thread is allowed in that block until that thread completes its
job no other thread is allowed to enter in to that block.
Syntax:
returnType methodName ( <<parms>>)
{
=====
synchronized(this)
{
//critical section
=====
}
======
}
Eg: double getBalance()
{
== ====
synchronized(this)
{ return balance; }
}
SSBN Degree College, ATP M Vishnuvardhan
Questions
Ad

More Related Content

What's hot (20)

Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
Haldia Institute of Technology
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Inter Thread Communicationn.pptx
Inter Thread Communicationn.pptxInter Thread Communicationn.pptx
Inter Thread Communicationn.pptx
SelvakumarNSNS
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
SURIT DATTA
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Java buzzwords
Java buzzwordsJava buzzwords
Java buzzwords
ramesh517
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Files in java
Files in javaFiles in java
Files in java
Muthukumaran Subramanian
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
Ravi Chythanya
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
Raja Sekhar
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 

Similar to Java Threads (20)

Multithreading
MultithreadingMultithreading
Multithreading
GowriLatha1
 
Threads
ThreadsThreads
Threads
QUAID-E-AWAM UNIVERSITY OF ENGINEERING, SCIENCE & TECHNOLOGY, NAWABSHAH, SINDH, PAKISTAN
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects javaModule - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
Rizwan Ali
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
Rizwan Ali
 
multithreading to be used in java with good programs.pptx
multithreading to be used in java with good programs.pptxmultithreading to be used in java with good programs.pptx
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Multithreading
MultithreadingMultithreading
Multithreading
SanthiNivas
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
this power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreadingthis power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreading
Betty333100
 
unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
sujatha629799
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
4 Multithreading and Parallel Programming.ppt
4 Multithreading and Parallel Programming.ppt4 Multithreading and Parallel Programming.ppt
4 Multithreading and Parallel Programming.ppt
MahmoudGad93
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
AmbigaMurugesan
 
Module - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects javaModule - 5 merged.docx notes about engineering subjects java
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
Rizwan Ali
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
Rizwan Ali
 
multithreading to be used in java with good programs.pptx
multithreading to be used in java with good programs.pptxmultithreading to be used in java with good programs.pptx
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
this power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreadingthis power point presentation is about concurrency and multithreading
this power point presentation is about concurrency and multithreading
Betty333100
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
4 Multithreading and Parallel Programming.ppt
4 Multithreading and Parallel Programming.ppt4 Multithreading and Parallel Programming.ppt
4 Multithreading and Parallel Programming.ppt
MahmoudGad93
 
Ad

More from M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
M Vishnuvardhan Reddy
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
M Vishnuvardhan Reddy
 
Python Control Structures.pptx
Python Control Structures.pptxPython Control Structures.pptx
Python Control Structures.pptx
M Vishnuvardhan Reddy
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
M Vishnuvardhan Reddy
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
M Vishnuvardhan Reddy
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
M Vishnuvardhan Reddy
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
M Vishnuvardhan Reddy
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
M Vishnuvardhan Reddy
 
Html forms
Html formsHtml forms
Html forms
M Vishnuvardhan Reddy
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
M Vishnuvardhan Reddy
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Scanner class
Scanner classScanner class
Scanner class
M Vishnuvardhan Reddy
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
M Vishnuvardhan Reddy
 
Java intro
Java introJava intro
Java intro
M Vishnuvardhan Reddy
 
Java applets
Java appletsJava applets
Java applets
M Vishnuvardhan Reddy
 
Exception handling
Exception handling Exception handling
Exception handling
M Vishnuvardhan Reddy
 
Control structures
Control structuresControl structures
Control structures
M Vishnuvardhan Reddy
 
Constructors
ConstructorsConstructors
Constructors
M Vishnuvardhan Reddy
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
M Vishnuvardhan Reddy
 
Shell sort
Shell sortShell sort
Shell sort
M Vishnuvardhan Reddy
 
Ad

Recently uploaded (20)

Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
Interactive odoo dashboards for sales, CRM , Inventory, Invoice, Purchase, Pr...
AxisTechnolabs
 
FL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full VersionFL Studio Producer Edition Crack 2025 Full Version
FL Studio Producer Edition Crack 2025 Full Version
tahirabibi60507
 
Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025Adobe Lightroom Classic Crack FREE Latest link 2025
Adobe Lightroom Classic Crack FREE Latest link 2025
kashifyounis067
 
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
F-Secure Freedome VPN 2025 Crack Plus Activation  New VersionF-Secure Freedome VPN 2025 Crack Plus Activation  New Version
F-Secure Freedome VPN 2025 Crack Plus Activation New Version
saimabibi60507
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
Landscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature ReviewLandscape of Requirements Engineering for/by AI through Literature Review
Landscape of Requirements Engineering for/by AI through Literature Review
Hironori Washizaki
 
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Mastering Fluent Bit: Ultimate Guide to Integrating Telemetry Pipelines with ...
Eric D. Schabell
 
Revolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptxRevolutionizing Residential Wi-Fi PPT.pptx
Revolutionizing Residential Wi-Fi PPT.pptx
nidhisingh691197
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025Why Orangescrum Is a Game Changer for Construction Companies in 2025
Why Orangescrum Is a Game Changer for Construction Companies in 2025
Orangescrum
 
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
What Do Contribution Guidelines Say About Software Testing? (MSR 2025)
Andre Hora
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
How Valletta helped healthcare SaaS to transform QA and compliance to grow wi...
Egor Kaleynik
 

Java Threads

  • 1. Threads In JAVA -M Vishnuvardhan, Dept. of Computer Science, SSBN Degree College, ATP
  • 2. SSBN Degree College, ATP M Vishnuvardhan Introduction The ability of an Operating Systems to perform multiple jobs concurrently is called as Multi-Tasking. Similarly in programming, we divide the program in to multiple units and execute them concurrently is called as Multi-Threading. Multi- Threading improves the performance of the Program and makes the programs to execute in a faster way Java Supports Multi-Threading.
  • 3. SSBN Degree College, ATP M Vishnuvardhan Thread – Defined A Thread is a piece of control which performs a job. A thread is considered as light weight process since it doesn't put burden on the CPU by demanding new resources when a new thread is created instead it shares the resources of its parent thread. But a process always burden on the CPU by demanding new resources when a new Process is created. In order to deal with the threads java defined Thread class in java.lang package
  • 4. SSBN Degree College, ATP M Vishnuvardhan Creation of Threads In java Threads can be created in two ways  By extending Thread class  By implementing Runnable interface class NumThread extends Thread { ==== } class NumThread implements Runnable { ==== }
  • 5. SSBN Degree College, ATP M Vishnuvardhan Creation of Threads Steps for creating thread by extending Thread class 1.Create a class by extending Thread class 2.Override run() method defined in Thread class. The run() is considered as heart of the Thread since it holds the job of the Thread 3.Create an object for the newly created thread 4.Start the thread using start() method present in the Thread class class NumThread extends Thread { public void run() { //job of the thread } } NumThread T1=new NumThread(); T1.start();
  • 6. SSBN Degree College, ATP M Vishnuvardhan Creation of Threads Steps for creating thread by implementing Runnable interface 1.Create a class by implementing Runnable interface 2.Override run() method declared in Runnable interface. 3.Create an object for the newly created class 4.Create a Thread class object by passing the reference of new created object 5.Now start the thread using start() method class NumThread implements Runnable { public void run() { //job of the thread } } NumThread T1=new NumThread(); T1.start(); // gives error Thread T2=new Thread(T1); T2.start();
  • 7. SSBN Degree College, ATP M Vishnuvardhan Life Cycle of the Thread start() suspend() sleep() wait() resume() notify() RunnableRunning New Born Blocked Dead yeild() stop() stop() stop()
  • 8. SSBN Degree College, ATP M Vishnuvardhan Thread Methods  void start(): used to start a Thread  void stop(): used to stop a Thread  void run() : used to specify the job of the Thread  void sleep( int milliseconds): used to suspend the Thread for a definite time  void suspend(): used to suspend a Thread for a indefinite time until the Thread uses resume() method  void resume(): used to resume a suspended thread  void wait(): used to suspend a thread until other Thread notifies it using notify()  void notify(): used to notify a waiting Thread.
  • 9. SSBN Degree College, ATP M Vishnuvardhan Thread Exceptions  IllegalThreadStateException: Occurs when a Thread is moved to invalid state of thread.  InterruptedException: Occurs when a Thread is interrupted. Generally sleep() generates this exception
  • 10. SSBN Degree College, ATP M Vishnuvardhan Thread Priority Priority is an integer number which is associated with each thread. Which specifies the priority of the Threads. In general threads contains three priorities. These are defined as constants in Thread class. MIN_PRIRORITY --- 1 NORM_PRIRORITY --- 5 MAX_PRIRORITY --- 10 Priority for threads can given using following methods int getPriority(): gets the priority of the thread void setPriority(int priority): sets the priority for the thread Eg: T1.setPriority(Thread.MAX_PRIORITY);
  • 11. SSBN Degree College, ATP M Vishnuvardhan Need of Synchronization Account accountNo Name Balance getBalance() Deposit() withDraw() Thread A Thread B deposit(5000) withDraw(3000)
  • 12. SSBN Degree College, ATP M Vishnuvardhan Synchronization It is common that two or more threads do the same job. This doesn't cause any problem as long as the job doesn’t contain critical section. A section is said to be critical section when the statements share common data. When multiple threads work on critical section some times they result in Data Integrity failure. Synchronization is a technique of allowing only one thread in to critical section at a time. It can be applied in two levels Method level Synchronization Block level Synchronization
  • 13. SSBN Degree College, ATP M Vishnuvardhan Method level Synchronization If all the statements inside a method are critical section then we apply synchronization at method level. i.e, only one thread is allowed in that method until that thread completes its job no other thread is allowed to enter in to the method. Syntax: synchronized returnType methodName ( <<parms>>) { ====== } Eg: synchronized double getBalance() { return balance; }
  • 14. SSBN Degree College, ATP M Vishnuvardhan Block level Synchronization If only few statements inside a method are critical section but not all the statements then we apply synchronization at block level. i.e, only one thread is allowed in that block until that thread completes its job no other thread is allowed to enter in to that block. Syntax: returnType methodName ( <<parms>>) { ===== synchronized(this) { //critical section ===== } ====== } Eg: double getBalance() { == ==== synchronized(this) { return balance; } }
  • 15. SSBN Degree College, ATP M Vishnuvardhan Questions