SlideShare a Scribd company logo
1 of 34
MultiThreading
By. Janmejaya
Agenda
Multithreading
Concepts
Creating
Thread
Controlling
Threads
Thread States
Summary
Multithreading
concepts
Need For Concurrency
Multi Processing
Multi Threading
Multithreading makes user-interface more
responsive
Better utilization of time
Creating
Threads
A thread represents
code that will be run
concurrently with other
code in the same
process.
Creating a Thread
• Use The Thread Class
• Use The Runnable Interface
Which
approach is
better?
Extending Thread is
Simpler.
Implementing
Runnable allows sub
classing other classes.
Extending
Thread
Extend
java.lang.Thread
Implement run
method
Implementing
Runnable
Create a Runnable Object
Create
Pass the Object to Thread Constructor
Pass
Override run method
Override
What Is
Thread
A Thread is a single sequential flow of control
within a program.
Thread does not have its own address space but
uses the memory and other resources of the
process in which it executes.
There may be several threads in one process.
The JVM manages this and shedules them.
What is
Multithreading?
Multithreading is a conceptual programming where
one program or process is divided into multiple sub
programs or tasks those are having independent
path of execution and can run in parallel.
For example one sub program can read a CSV file
and another sub program can enter the data into
DB.
A program that contains multiple flow of control is
known as multithreaded program.
Thread State
Thread
Priorities
Java assign each thread a priority, that
determines how that Thread should be
treated with respect to others.
Thread Priorities are integers that
specify relative priority of one thread to
another.
A Thread Priority is used to decide when
to switch from one running thread to the
next. This is called context switching.
Thread Priorities
• A thread can voluntarily
relinquish control. This is done by
explicit yielding, sleeping or
blocking on pending IO. In this
scenario all other threads are
examined , and the highest
priority thread that is ready to
run is given the CPU.
• A Thread can be pre-empted by a
higher priority thread. This is
called pre-emptive multi tasking.
Multitasking
Multitasking allows several activities to
occur concurrently on the computer. A
distinction is usually made between :
Process based Multitasking
Thread based Multitasking
Advantage of
thread based
multithreading
Thread share the
same address
space
Context switching
is less expensive.
Cost of
communication
between threads is
relatively low
Overview of
Thread
A thread is an independent sequential
path of execution within a program.
Many threads can run concurrently
within a program.
At run time Threads in a program exists
in a common memory space and can
therefore share both data and code.
The Main
Thread
When a standalone application run, a user thread is
automatically created to execute the main method of the
application.
From main thread child thread are spawned.
If no user threads are there ,the program terminates when
main execution finished.
All other threads are called child threads ,spawned from main
thread inheriting its child thread status.
The main method can finish , but the program will keep
running until all user threads have complted.
User Threads & Daemon Threads
The run time environment distinguishes between user threads and
daemon threads. As long as user thread is alive , the JVM does not
terminate.
A daemon thread is at the mercy of the runtime system. It is stopped if
there are no more user threads running, thus terminating the program.
Daemon thread exists only to serve user thread.
Creating
Daemon or
User Thread
Calling the setDaemon(boolean) method
of Thread class marks the status of
Thread as either Daemon or User.
This method must be called on a Thread
object before it starts.
Any attempt to change the status of the
Thread will throw an
IllegalThreadStateException.
Thread
Creation
A Thread in Java is represented by
an object of the Thread class.
Implementing thread is achieved in
one of the two ways.
Implementing the
java.lang.Runnable interface
Extending the java.lang.Thread
class
Thread class method
Thread class method
Thread class
constructor
Thread class
constructor
Synchronization
Threads share the same memory space i.e they can share resources.
However there are some critical situation where it is desirable that only one Thread
at a time has access to a shared resources.
For example crediting and debiting a shared bank account concurrently among
several users without proper discipline will harm the integrity of the account data.
Java Provides high level concepts of synchronization in order to control access to
shared resources.
Locks
A lock also called as a monitor is used to synchronize access
to a shared resource.
Threads gain access to a shared resource by first acquiring the
lock associated with the resource.
At any given time at most one thread can hold the lock and
there by have access to the shared resource.
In Java , all the objects have a lock, including arrays.
By associating a shared resource with a java object and its
lock , the object can act as a guard, ensuring synchronized
access to the resource.
Object Lock
Mechanism
• The Object lock mechanism , enforces the following rules of
synchronization.
• A thread must acquire the object lock
associated with a shared resource, before it can
enter the shared resource. The runtime system
ensures that no other thread can entered a
shared resource if another thread already holds
the object lock associated with it.
• If a thread can not immediately acquire the
object lock, it will be blocked.
• If a thread exists a shared resource , the runtime
ensures that the object lock is also relinquished.
If another thread is waiting for this object lock ,
it will be notified .
Two ways of
synchronization
• There are two ways in which
execution of code can be
synchronized:
• Synchronized Method
• Synchronized Code block
Different
way to hold
a lock
By executing a synchronized
instance method of the object.
By executing a synchronized block
that synchronizes on the object.
By executing a synchronized static
method of a class .
Thread States
New state-: A thread is created but not yet started.
Ready To run state-: when we call start() on a Thread object comes to this state waiting for TS to
get its turn.
Running state-: In this state the thread gets its turn and started executing run ().
Dead State-:Once in this state, A thread can not run again.
Non-Runnable states-: A running thread can transit to one of the non-runnable states. A thread
remains in non-runnable state until a special transition occurs.
Non-
Runnable
States
Sleeping: the thread sleeps for a specified
amount of time.
Blocked for I/O: the threads wait for
blocking operation to complete.
Blocked for join completion: The thread
awaits completion of another thread.
Waiting for notification: the thread awaits
notification from another thread.
Blocked for lock acquisition: The thread wait
to acquire the lock.
Thread.State
NEW – Created but
not yet started
RUNNABLE –
Executing the JVM
BLOCKED – blocked
for lock acquisition
WAITING- Waiting
for notify, Blocked
for join completion
TIMED_WAITING-
Sleeping, Waiting
For notify
TERMINATED-
Completed
Thread
Prorities
Threads are assigned priorities that the thread scheduler can
use to determine how the threads will be scheduled.
The TS can use thread priorities to determine which thread
gets to run.
The TS favours giving the CPU time to the thread with the
highest priority in ready to run state.
Heavy reliance on thread priorities for the behavior of a
program can make the program unportable across platforms,
as thread scheduling is host-platform dependant.
Thread
Priorities
Priorities are integer values
1-lowest (Thread.MIN_PRIORITY)
10-highest(Thread.MAX_PRIORITY)
5-normal(Thread.NORM_PRIORITY)
Thread Scheduler
Scheduler in JVM implementations usually employ one of the two
strategies.
Preemptive scheduling: If a thread with a higher priority than the current
running thread moves to the ready to run state, then the current state can
be preempted to the higher priority thread execute.
Time-sliced or Round-Robin: A running thread is allowed to execute for a
fixed length of time , after which it moves to ready to run state.

More Related Content

What's hot

Spring Framework Tutorial | VirtualNuggets
Spring Framework Tutorial | VirtualNuggetsSpring Framework Tutorial | VirtualNuggets
Spring Framework Tutorial | VirtualNuggetsVirtual Nuggets
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework tola99
 
Introduction to Ibatis by Rohit
Introduction to Ibatis by RohitIntroduction to Ibatis by Rohit
Introduction to Ibatis by RohitRohit Prabhakar
 
Java & J2EE Struts with Hibernate Framework
Java & J2EE Struts with Hibernate FrameworkJava & J2EE Struts with Hibernate Framework
Java & J2EE Struts with Hibernate FrameworkMohit Belwal
 
Spring core module
Spring core moduleSpring core module
Spring core moduleRaj Tomar
 
Spring framework-tutorial
Spring framework-tutorialSpring framework-tutorial
Spring framework-tutorialvinayiqbusiness
 
Spring framework Introduction
Spring framework  IntroductionSpring framework  Introduction
Spring framework IntroductionAnuj Singh Rajput
 
Building Enterprise Application with J2EE
Building Enterprise Application with J2EEBuilding Enterprise Application with J2EE
Building Enterprise Application with J2EECalance
 
Introduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCIntroduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCFunnelll
 
Java spring framework
Java spring frameworkJava spring framework
Java spring frameworkRajiv Gupta
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC frameworkMohit Gupta
 
CR Bridge Solutions Pvt Ltd. Java slides
CR Bridge Solutions Pvt Ltd. Java slidesCR Bridge Solutions Pvt Ltd. Java slides
CR Bridge Solutions Pvt Ltd. Java slidesCRBTech
 

What's hot (20)

Spring Framework Tutorial | VirtualNuggets
Spring Framework Tutorial | VirtualNuggetsSpring Framework Tutorial | VirtualNuggets
Spring Framework Tutorial | VirtualNuggets
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Introduction to Ibatis by Rohit
Introduction to Ibatis by RohitIntroduction to Ibatis by Rohit
Introduction to Ibatis by Rohit
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Java & J2EE Struts with Hibernate Framework
Java & J2EE Struts with Hibernate FrameworkJava & J2EE Struts with Hibernate Framework
Java & J2EE Struts with Hibernate Framework
 
Spring core module
Spring core moduleSpring core module
Spring core module
 
Java Spring
Java SpringJava Spring
Java Spring
 
Spring framework-tutorial
Spring framework-tutorialSpring framework-tutorial
Spring framework-tutorial
 
Java spring ppt
Java spring pptJava spring ppt
Java spring ppt
 
Spring Framework Rohit
Spring Framework RohitSpring Framework Rohit
Spring Framework Rohit
 
Spring framework Introduction
Spring framework  IntroductionSpring framework  Introduction
Spring framework Introduction
 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Building Enterprise Application with J2EE
Building Enterprise Application with J2EEBuilding Enterprise Application with J2EE
Building Enterprise Application with J2EE
 
J2EE Introduction
J2EE IntroductionJ2EE Introduction
J2EE Introduction
 
Introduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoCIntroduction to Spring Framework and Spring IoC
Introduction to Spring Framework and Spring IoC
 
Java spring framework
Java spring frameworkJava spring framework
Java spring framework
 
Spring MVC framework
Spring MVC frameworkSpring MVC framework
Spring MVC framework
 
J2ee seminar
J2ee seminarJ2ee seminar
J2ee seminar
 
CR Bridge Solutions Pvt Ltd. Java slides
CR Bridge Solutions Pvt Ltd. Java slidesCR Bridge Solutions Pvt Ltd. Java slides
CR Bridge Solutions Pvt Ltd. Java slides
 

Similar to Multithreading in java

Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
Java multithreading
Java multithreadingJava multithreading
Java multithreadingMohammed625
 
Multithreading
MultithreadingMultithreading
MultithreadingF K
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
Multithreading
MultithreadingMultithreading
Multithreadingsagsharma
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in javaDucat India
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptxmadan r
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptxsandhyakiran10
 

Similar to Multithreading in java (20)

Multi threading
Multi threadingMulti threading
Multi threading
 
Java threading
Java threadingJava threading
Java threading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java
JavaJava
Java
 
Java
JavaJava
Java
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
multithreading
multithreadingmultithreading
multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Thread priorities in java
Thread priorities in javaThread priorities in java
Thread priorities in java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
 

Recently uploaded

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 

Recently uploaded (20)

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 

Multithreading in java

  • 3. Multithreading concepts Need For Concurrency Multi Processing Multi Threading Multithreading makes user-interface more responsive Better utilization of time
  • 4. Creating Threads A thread represents code that will be run concurrently with other code in the same process. Creating a Thread • Use The Thread Class • Use The Runnable Interface
  • 5. Which approach is better? Extending Thread is Simpler. Implementing Runnable allows sub classing other classes.
  • 7. Implementing Runnable Create a Runnable Object Create Pass the Object to Thread Constructor Pass Override run method Override
  • 8. What Is Thread A Thread is a single sequential flow of control within a program. Thread does not have its own address space but uses the memory and other resources of the process in which it executes. There may be several threads in one process. The JVM manages this and shedules them.
  • 9. What is Multithreading? Multithreading is a conceptual programming where one program or process is divided into multiple sub programs or tasks those are having independent path of execution and can run in parallel. For example one sub program can read a CSV file and another sub program can enter the data into DB. A program that contains multiple flow of control is known as multithreaded program.
  • 11. Thread Priorities Java assign each thread a priority, that determines how that Thread should be treated with respect to others. Thread Priorities are integers that specify relative priority of one thread to another. A Thread Priority is used to decide when to switch from one running thread to the next. This is called context switching.
  • 12. Thread Priorities • A thread can voluntarily relinquish control. This is done by explicit yielding, sleeping or blocking on pending IO. In this scenario all other threads are examined , and the highest priority thread that is ready to run is given the CPU. • A Thread can be pre-empted by a higher priority thread. This is called pre-emptive multi tasking.
  • 13. Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between : Process based Multitasking Thread based Multitasking
  • 14. Advantage of thread based multithreading Thread share the same address space Context switching is less expensive. Cost of communication between threads is relatively low
  • 15. Overview of Thread A thread is an independent sequential path of execution within a program. Many threads can run concurrently within a program. At run time Threads in a program exists in a common memory space and can therefore share both data and code.
  • 16. The Main Thread When a standalone application run, a user thread is automatically created to execute the main method of the application. From main thread child thread are spawned. If no user threads are there ,the program terminates when main execution finished. All other threads are called child threads ,spawned from main thread inheriting its child thread status. The main method can finish , but the program will keep running until all user threads have complted.
  • 17. User Threads & Daemon Threads The run time environment distinguishes between user threads and daemon threads. As long as user thread is alive , the JVM does not terminate. A daemon thread is at the mercy of the runtime system. It is stopped if there are no more user threads running, thus terminating the program. Daemon thread exists only to serve user thread.
  • 18. Creating Daemon or User Thread Calling the setDaemon(boolean) method of Thread class marks the status of Thread as either Daemon or User. This method must be called on a Thread object before it starts. Any attempt to change the status of the Thread will throw an IllegalThreadStateException.
  • 19. Thread Creation A Thread in Java is represented by an object of the Thread class. Implementing thread is achieved in one of the two ways. Implementing the java.lang.Runnable interface Extending the java.lang.Thread class
  • 24. Synchronization Threads share the same memory space i.e they can share resources. However there are some critical situation where it is desirable that only one Thread at a time has access to a shared resources. For example crediting and debiting a shared bank account concurrently among several users without proper discipline will harm the integrity of the account data. Java Provides high level concepts of synchronization in order to control access to shared resources.
  • 25. Locks A lock also called as a monitor is used to synchronize access to a shared resource. Threads gain access to a shared resource by first acquiring the lock associated with the resource. At any given time at most one thread can hold the lock and there by have access to the shared resource. In Java , all the objects have a lock, including arrays. By associating a shared resource with a java object and its lock , the object can act as a guard, ensuring synchronized access to the resource.
  • 26. Object Lock Mechanism • The Object lock mechanism , enforces the following rules of synchronization. • A thread must acquire the object lock associated with a shared resource, before it can enter the shared resource. The runtime system ensures that no other thread can entered a shared resource if another thread already holds the object lock associated with it. • If a thread can not immediately acquire the object lock, it will be blocked. • If a thread exists a shared resource , the runtime ensures that the object lock is also relinquished. If another thread is waiting for this object lock , it will be notified .
  • 27. Two ways of synchronization • There are two ways in which execution of code can be synchronized: • Synchronized Method • Synchronized Code block
  • 28. Different way to hold a lock By executing a synchronized instance method of the object. By executing a synchronized block that synchronizes on the object. By executing a synchronized static method of a class .
  • 29. Thread States New state-: A thread is created but not yet started. Ready To run state-: when we call start() on a Thread object comes to this state waiting for TS to get its turn. Running state-: In this state the thread gets its turn and started executing run (). Dead State-:Once in this state, A thread can not run again. Non-Runnable states-: A running thread can transit to one of the non-runnable states. A thread remains in non-runnable state until a special transition occurs.
  • 30. Non- Runnable States Sleeping: the thread sleeps for a specified amount of time. Blocked for I/O: the threads wait for blocking operation to complete. Blocked for join completion: The thread awaits completion of another thread. Waiting for notification: the thread awaits notification from another thread. Blocked for lock acquisition: The thread wait to acquire the lock.
  • 31. Thread.State NEW – Created but not yet started RUNNABLE – Executing the JVM BLOCKED – blocked for lock acquisition WAITING- Waiting for notify, Blocked for join completion TIMED_WAITING- Sleeping, Waiting For notify TERMINATED- Completed
  • 32. Thread Prorities Threads are assigned priorities that the thread scheduler can use to determine how the threads will be scheduled. The TS can use thread priorities to determine which thread gets to run. The TS favours giving the CPU time to the thread with the highest priority in ready to run state. Heavy reliance on thread priorities for the behavior of a program can make the program unportable across platforms, as thread scheduling is host-platform dependant.
  • 33. Thread Priorities Priorities are integer values 1-lowest (Thread.MIN_PRIORITY) 10-highest(Thread.MAX_PRIORITY) 5-normal(Thread.NORM_PRIORITY)
  • 34. Thread Scheduler Scheduler in JVM implementations usually employ one of the two strategies. Preemptive scheduling: If a thread with a higher priority than the current running thread moves to the ready to run state, then the current state can be preempted to the higher priority thread execute. Time-sliced or Round-Robin: A running thread is allowed to execute for a fixed length of time , after which it moves to ready to run state.