SlideShare a Scribd company logo
1 of 15
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

More Related Content

What's hot (20)

Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Java String
Java String Java String
Java String
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String in java
String in javaString in java
String in java
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 

Similar to Java Threads (20)

Multithreading
MultithreadingMultithreading
Multithreading
 
Threads
ThreadsThreads
Threads
 
Threads and Synchronization in c#
Threads and Synchronization in c#Threads and Synchronization in c#
Threads and Synchronization in c#
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
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
 
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
 
unit-3java.pptx
unit-3java.pptxunit-3java.pptx
unit-3java.pptx
 
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
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

More from M Vishnuvardhan Reddy (20)

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

Recently uploaded

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 

Recently uploaded (20)

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 

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