SlideShare a Scribd company logo

THREADING CONCEPTS
by
U.Raheema parveen

 Light weight process
 Smallest unit of processing
 Share some memory area of the process
 Thread is executed inside the process
 Executing multi threads simultaneously
WHAT IS THREAD

WHAT IS MULTITHREADING
Multi processing Multi threading
Separate memory space Share same memory
Heavy weight Light weight process
Time and cost is more Less cost and time

 Threads are light weight process
Because they share same memory space for child
threads too. whereas process , do not allocate same
memory space.
 Interthread communication is in expensive
 Context switching of threads are in expensive
 Process inside the interthread and context sensitive is
costly
Advantages of threads

 Threads are implemented in the form of objects that
contain a method is called run() method is the heart
and soul of any thread
public void run()
{
………. (statement for implementing thread)
……….
}
Creating threads

 The run() method should be invoked by an object of the
concerned thread.
 This can be achieved by creating the thread the initiating it
with help of another thread method called start().
 Following two ways
By creating a thread class: A class that extends thread class and
override its run() method with the code required method
Creating threads-con’d

 By converting a class to a thread: Define a class that
implements Runnable interface. The Runnable
interface has only one method , run() that is to be
defined in the method the code to be executed by the
thread
Creating threads-con’d
 Declare the class as extending the thread class
 Implement the run() method
 Create a thread object and call the start() method to initiate the
thread execution
Declaring the class
the thread class can be extended as follows
Class MyThread extends Thread
{
………….
………….
………….
}
Extending the thread class

Implementing the run() method:
 The run() method has been inherited by the Mythread.
 This method in order to implement the code to be executed by
our thread.
 The basic implementation of run() like
public void run()
{
………
………
………
}
Extending the thread class

Starting new thread:
to actually create and run an instance of our thread class
MyThread aThread = new MyThread();
aThread.start();
 New objects of class MyThread, the thread will be
Newborn state.
 The start() method to move into the runnable state
Extending the thread class
class MultithreadingDemo extends Thread{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
obj.start();
} }
Output:
 My thread is in running state.
Example program

Life cycle
 We create a thread object, the
thread is born and is said to be
in newborn state.
 The thread is not yet scheduled
for running
Following things
 Schedule it for running using start() method
 Kill it using stop() method
Life cycle – New born state

 The running state means that the thread is ready for execution
and is waiting for the availability of the process
 The thread has joined the queue of threads they are waiting for
execution
 The process of assigning time to thread is called a time slicing
 We can using the yield() method
Life cycle – Runnable state

Running means the process has been time to the thread for
execution
Following situations
1. It has been suspended using suspend() method. A
suspended thread can be revived by using the resume()
method.
2. It has been made to sleep. We can a thread to sleep for a
specified time period using the method sleep(time).where
time is in milliseconds
Life cycle –Running state

3. It has been hold to wait until some event occurs. This
is done using wait() method. The thread can be
scheduled to run again for the notify() method.
Running state-con’d

 A thread is said to be blocked when it is prevented
from entering into the runnable state and
subsequently the running state.
 The thread is suspended, sleeping, or waiting in
order to satisfy certain requirement.
 A blocked thread is “not runnable” but not dead it
has access to run again.
Life cycle-blocked state

 A running thread ends its life when it has completed
executing its run() method.
 It is natural death
 A thread can be killed as soon it is born , or while it
is running ,or even when it is “not runnable”
(blocked) condition.
Life cycle-Dead state

 It can be default running
 When a program begins , one thread begins running
immediately
Importance of main threads
 A thread from which other “child” thread can be created
 Must be the threads to finish execution
 Current Thread() returns reference to the current thread
 It is public and static in nature thread objects
Main thread

 getName – get in thread name
 getPriority – thread get priorities
 isAlive – check if a thread is still running
 Join – wait for a thread to terminated
 run – entry point for the thread
 sleep – suspend a thread for a period of time
 start() – start a thread by calling its run method
Thread methods

 The call to sleep() method is enclosed in a try block
and followed by a catch block. This is necessary
because the sleep() method throws an exception. if
we fail to catch the exception, program will not
compile.
 Syntax
Catch(threadDeath e) {
……..
……..
}
Thread execptions
Catch(InterruptedException e)
{
………..
………..
}
Catch(Illegal ArgumentException e)
{
………..
………..
}
Catch(Exception e)
{
………..
………..
}
Thread exceptions-con’d

 thread scheduler assigns processor to a thread based on
priority of thread.
 Whenever we create a thread in Java, it always has some
priority assigned to it.
 Java is set the priority is using method is setpriority()
method.
ThreadName.setPriority(intNumber);
The intNumber is an integer value
Thread priority

 public static int MIN_PRIORITY: This is minimum
priority that a thread can have. Value for this is 1.
 public static int NORM_PRIORITY: This is default
priority of a thread if do not explicitly define it.
Value for this is 5
 public static int MAX_PRIORITY: This is maximum
priority of a thread. Value for this is 10.
Priority-con’d
 Capability to control the access of multiple threads to a
shared resource
Why?
 To prevent the interference of threads
 To prevent consistency problems.
 Understanding locks
 Every object has a lock or monitor.
 A thread needs to acquire lock before accessing objects
Types
Process ,Thread
synchronization
Mutual extension Co-operation
Keeps threads from
interfering with one another
while sharing data
 Synchronized method
 Synchronized block
 Static synchronization
Inter-thread communication
 synchronized threads to
communicate with each
others
Thread synchronization

 We can create two ways
 Using the extended Thread class
 Implementing the Runnable interface
The Runnable interface declares the run()
Method that is required for implementing threads in
programs.
Runnable interface

 Following steps
 Declare the class as implementing the Runnable
interface.
 Implement the run() method.
 Create a thread by defining an object that is
instantiated from “runnable” class as the target of
the thread
 Call the thread start() method to run the thread.
Runnable interface
class MultithreadingDemo implements Runnable
{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
Thread tobj=new Thread(obj);
tobj.start();
} }
Output:
 My thread is in running state.
Runnable interface-example program

Thank you
Ad

More Related Content

What's hot (20)

Developing Multithreaded Applications
Developing Multithreaded ApplicationsDeveloping Multithreaded Applications
Developing Multithreaded Applications
Bharat17485
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-java
aalipalh
 
Multi threading
Multi threadingMulti threading
Multi threading
Mavoori Soshmitha
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
myrajendra
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
Arvind Krishnaa
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
javaicon
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
myrajendra
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Java thread
Java threadJava thread
Java thread
Arati Gadgil
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
Shipra Swati
 
javathreads
javathreadsjavathreads
javathreads
Arjun Shanka
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
Thread
ThreadThread
Thread
Juhi Kumari
 
Developing Multithreaded Applications
Developing Multithreaded ApplicationsDeveloping Multithreaded Applications
Developing Multithreaded Applications
Bharat17485
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-java
aalipalh
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
myrajendra
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
javaicon
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
myrajendra
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 

Similar to Threading concepts (20)

multhi threading concept in oops through java
multhi threading concept in oops through javamulthi threading concept in oops through java
multhi threading concept in oops through java
Parameshwar Maddela
 
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
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
CSE 3146 M1- MULTI THREADING USING JAVA  .pdfCSE 3146 M1- MULTI THREADING USING JAVA  .pdf
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
multithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptxmultithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
nehakumari0xf
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
kashyapneha2809
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
MSBTE Computer Engineering JPR java. multi. threading.pptx
MSBTE Computer Engineering  JPR java.  multi. threading.pptxMSBTE Computer Engineering  JPR java.  multi. threading.pptx
MSBTE Computer Engineering JPR java. multi. threading.pptx
kunalgaikwad1705
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
PragatiSutar4
 
multhi threading concept in oops through java
multhi threading concept in oops through javamulthi threading concept in oops through java
multhi threading concept in oops through java
Parameshwar Maddela
 
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
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
CSE 3146 M1- MULTI THREADING USING JAVA  .pdfCSE 3146 M1- MULTI THREADING USING JAVA  .pdf
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
multithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptxmultithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
nehakumari0xf
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
kashyapneha2809
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
MSBTE Computer Engineering JPR java. multi. threading.pptx
MSBTE Computer Engineering  JPR java.  multi. threading.pptxMSBTE Computer Engineering  JPR java.  multi. threading.pptx
MSBTE Computer Engineering JPR java. multi. threading.pptx
kunalgaikwad1705
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Ad

Recently uploaded (20)

Applications of Radioisotopes in Cancer Research.pptx
Applications of Radioisotopes in Cancer Research.pptxApplications of Radioisotopes in Cancer Research.pptx
Applications of Radioisotopes in Cancer Research.pptx
MahitaLaveti
 
Top 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptxTop 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptx
alexbagheriam
 
dsDNA-ASF, asfaviridae, virus in virology presentation
dsDNA-ASF, asfaviridae, virus in virology presentationdsDNA-ASF, asfaviridae, virus in virology presentation
dsDNA-ASF, asfaviridae, virus in virology presentation
JessaMaeDacayo
 
CORONARY ARTERY BYPASS GRAFTING (1).pptx
CORONARY ARTERY BYPASS GRAFTING (1).pptxCORONARY ARTERY BYPASS GRAFTING (1).pptx
CORONARY ARTERY BYPASS GRAFTING (1).pptx
DharaniJajula
 
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
Sérgio Sacani
 
A CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptx
A CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptxA CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptx
A CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptx
ANJALICHANDRASEKARAN
 
Introduction to Black Hole and how its formed
Introduction to Black Hole and how its formedIntroduction to Black Hole and how its formed
Introduction to Black Hole and how its formed
MSafiullahALawi
 
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...
Sérgio Sacani
 
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.pptSULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
HRUTUJA WAGH
 
Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)
memesologiesxd
 
Secondary metabolite ,Plants and Health Care
Secondary metabolite ,Plants and Health CareSecondary metabolite ,Plants and Health Care
Secondary metabolite ,Plants and Health Care
Nistarini College, Purulia (W.B) India
 
Reticular formation_groups_organization_
Reticular formation_groups_organization_Reticular formation_groups_organization_
Reticular formation_groups_organization_
klynct
 
Brief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdfBrief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdf
BharathKumar556689
 
Fatigue and its management in aviation medicine
Fatigue and its management in aviation medicineFatigue and its management in aviation medicine
Fatigue and its management in aviation medicine
ImranJewel2
 
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptxSiver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
PriyaAntil3
 
Freshwater Biome Types, Characteristics and Factors
Freshwater Biome Types, Characteristics and FactorsFreshwater Biome Types, Characteristics and Factors
Freshwater Biome Types, Characteristics and Factors
mytriplemonlineshop
 
Eric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptxEric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptx
ttalbert1
 
Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...
Sérgio Sacani
 
Anti fungal agents Medicinal Chemistry III
Anti fungal agents Medicinal Chemistry  IIIAnti fungal agents Medicinal Chemistry  III
Anti fungal agents Medicinal Chemistry III
HRUTUJA WAGH
 
Mycology:Characteristics of Ascomycetes Fungi
Mycology:Characteristics of Ascomycetes FungiMycology:Characteristics of Ascomycetes Fungi
Mycology:Characteristics of Ascomycetes Fungi
SAYANTANMALLICK5
 
Applications of Radioisotopes in Cancer Research.pptx
Applications of Radioisotopes in Cancer Research.pptxApplications of Radioisotopes in Cancer Research.pptx
Applications of Radioisotopes in Cancer Research.pptx
MahitaLaveti
 
Top 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptxTop 10 Biotech Startups for Beginners.pptx
Top 10 Biotech Startups for Beginners.pptx
alexbagheriam
 
dsDNA-ASF, asfaviridae, virus in virology presentation
dsDNA-ASF, asfaviridae, virus in virology presentationdsDNA-ASF, asfaviridae, virus in virology presentation
dsDNA-ASF, asfaviridae, virus in virology presentation
JessaMaeDacayo
 
CORONARY ARTERY BYPASS GRAFTING (1).pptx
CORONARY ARTERY BYPASS GRAFTING (1).pptxCORONARY ARTERY BYPASS GRAFTING (1).pptx
CORONARY ARTERY BYPASS GRAFTING (1).pptx
DharaniJajula
 
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
A Massive Black Hole 0.8kpc from the Host Nucleus Revealed by the Offset Tida...
Sérgio Sacani
 
A CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptx
A CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptxA CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptx
A CASE OF MULTINODULAR GOITRE,clinical presentation and management.pptx
ANJALICHANDRASEKARAN
 
Introduction to Black Hole and how its formed
Introduction to Black Hole and how its formedIntroduction to Black Hole and how its formed
Introduction to Black Hole and how its formed
MSafiullahALawi
 
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...
Evidence for a polar circumbinary exoplanet orbiting a pair of eclipsing brow...
Sérgio Sacani
 
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.pptSULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
SULPHONAMIDES AND SULFONES Medicinal Chemistry III.ppt
HRUTUJA WAGH
 
Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)Study in Pink (forensic case study of Death)
Study in Pink (forensic case study of Death)
memesologiesxd
 
Reticular formation_groups_organization_
Reticular formation_groups_organization_Reticular formation_groups_organization_
Reticular formation_groups_organization_
klynct
 
Brief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdfBrief Presentation on Garment Washing.pdf
Brief Presentation on Garment Washing.pdf
BharathKumar556689
 
Fatigue and its management in aviation medicine
Fatigue and its management in aviation medicineFatigue and its management in aviation medicine
Fatigue and its management in aviation medicine
ImranJewel2
 
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptxSiver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
Siver Nanoparticles syntheisis, mechanism, Antibacterial activity.pptx
PriyaAntil3
 
Freshwater Biome Types, Characteristics and Factors
Freshwater Biome Types, Characteristics and FactorsFreshwater Biome Types, Characteristics and Factors
Freshwater Biome Types, Characteristics and Factors
mytriplemonlineshop
 
Eric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptxEric Schott- Environment, Animal and Human Health (3).pptx
Eric Schott- Environment, Animal and Human Health (3).pptx
ttalbert1
 
Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...Astrobiological implications of the stability andreactivity of peptide nuclei...
Astrobiological implications of the stability andreactivity of peptide nuclei...
Sérgio Sacani
 
Anti fungal agents Medicinal Chemistry III
Anti fungal agents Medicinal Chemistry  IIIAnti fungal agents Medicinal Chemistry  III
Anti fungal agents Medicinal Chemistry III
HRUTUJA WAGH
 
Mycology:Characteristics of Ascomycetes Fungi
Mycology:Characteristics of Ascomycetes FungiMycology:Characteristics of Ascomycetes Fungi
Mycology:Characteristics of Ascomycetes Fungi
SAYANTANMALLICK5
 
Ad

Threading concepts

  • 2.   Light weight process  Smallest unit of processing  Share some memory area of the process  Thread is executed inside the process  Executing multi threads simultaneously WHAT IS THREAD
  • 3.  WHAT IS MULTITHREADING Multi processing Multi threading Separate memory space Share same memory Heavy weight Light weight process Time and cost is more Less cost and time
  • 4.   Threads are light weight process Because they share same memory space for child threads too. whereas process , do not allocate same memory space.  Interthread communication is in expensive  Context switching of threads are in expensive  Process inside the interthread and context sensitive is costly Advantages of threads
  • 5.   Threads are implemented in the form of objects that contain a method is called run() method is the heart and soul of any thread public void run() { ………. (statement for implementing thread) ………. } Creating threads
  • 6.   The run() method should be invoked by an object of the concerned thread.  This can be achieved by creating the thread the initiating it with help of another thread method called start().  Following two ways By creating a thread class: A class that extends thread class and override its run() method with the code required method Creating threads-con’d
  • 7.   By converting a class to a thread: Define a class that implements Runnable interface. The Runnable interface has only one method , run() that is to be defined in the method the code to be executed by the thread Creating threads-con’d
  • 8.  Declare the class as extending the thread class  Implement the run() method  Create a thread object and call the start() method to initiate the thread execution Declaring the class the thread class can be extended as follows Class MyThread extends Thread { …………. …………. …………. } Extending the thread class
  • 9.  Implementing the run() method:  The run() method has been inherited by the Mythread.  This method in order to implement the code to be executed by our thread.  The basic implementation of run() like public void run() { ……… ……… ……… } Extending the thread class
  • 10.  Starting new thread: to actually create and run an instance of our thread class MyThread aThread = new MyThread(); aThread.start();  New objects of class MyThread, the thread will be Newborn state.  The start() method to move into the runnable state Extending the thread class
  • 11. class MultithreadingDemo extends Thread{ public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); obj.start(); } } Output:  My thread is in running state. Example program
  • 13.  We create a thread object, the thread is born and is said to be in newborn state.  The thread is not yet scheduled for running Following things  Schedule it for running using start() method  Kill it using stop() method Life cycle – New born state
  • 14.   The running state means that the thread is ready for execution and is waiting for the availability of the process  The thread has joined the queue of threads they are waiting for execution  The process of assigning time to thread is called a time slicing  We can using the yield() method Life cycle – Runnable state
  • 15.  Running means the process has been time to the thread for execution Following situations 1. It has been suspended using suspend() method. A suspended thread can be revived by using the resume() method. 2. It has been made to sleep. We can a thread to sleep for a specified time period using the method sleep(time).where time is in milliseconds Life cycle –Running state
  • 16.  3. It has been hold to wait until some event occurs. This is done using wait() method. The thread can be scheduled to run again for the notify() method. Running state-con’d
  • 17.   A thread is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state.  The thread is suspended, sleeping, or waiting in order to satisfy certain requirement.  A blocked thread is “not runnable” but not dead it has access to run again. Life cycle-blocked state
  • 18.   A running thread ends its life when it has completed executing its run() method.  It is natural death  A thread can be killed as soon it is born , or while it is running ,or even when it is “not runnable” (blocked) condition. Life cycle-Dead state
  • 19.   It can be default running  When a program begins , one thread begins running immediately Importance of main threads  A thread from which other “child” thread can be created  Must be the threads to finish execution  Current Thread() returns reference to the current thread  It is public and static in nature thread objects Main thread
  • 20.   getName – get in thread name  getPriority – thread get priorities  isAlive – check if a thread is still running  Join – wait for a thread to terminated  run – entry point for the thread  sleep – suspend a thread for a period of time  start() – start a thread by calling its run method Thread methods
  • 21.   The call to sleep() method is enclosed in a try block and followed by a catch block. This is necessary because the sleep() method throws an exception. if we fail to catch the exception, program will not compile.  Syntax Catch(threadDeath e) { …….. …….. } Thread execptions
  • 22. Catch(InterruptedException e) { ……….. ……….. } Catch(Illegal ArgumentException e) { ……….. ……….. } Catch(Exception e) { ……….. ……….. } Thread exceptions-con’d
  • 23.   thread scheduler assigns processor to a thread based on priority of thread.  Whenever we create a thread in Java, it always has some priority assigned to it.  Java is set the priority is using method is setpriority() method. ThreadName.setPriority(intNumber); The intNumber is an integer value Thread priority
  • 24.   public static int MIN_PRIORITY: This is minimum priority that a thread can have. Value for this is 1.  public static int NORM_PRIORITY: This is default priority of a thread if do not explicitly define it. Value for this is 5  public static int MAX_PRIORITY: This is maximum priority of a thread. Value for this is 10. Priority-con’d
  • 25.  Capability to control the access of multiple threads to a shared resource Why?  To prevent the interference of threads  To prevent consistency problems.  Understanding locks  Every object has a lock or monitor.  A thread needs to acquire lock before accessing objects Types Process ,Thread synchronization
  • 26. Mutual extension Co-operation Keeps threads from interfering with one another while sharing data  Synchronized method  Synchronized block  Static synchronization Inter-thread communication  synchronized threads to communicate with each others Thread synchronization
  • 27.   We can create two ways  Using the extended Thread class  Implementing the Runnable interface The Runnable interface declares the run() Method that is required for implementing threads in programs. Runnable interface
  • 28.   Following steps  Declare the class as implementing the Runnable interface.  Implement the run() method.  Create a thread by defining an object that is instantiated from “runnable” class as the target of the thread  Call the thread start() method to run the thread. Runnable interface
  • 29. class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread tobj=new Thread(obj); tobj.start(); } } Output:  My thread is in running state. Runnable interface-example program
  翻译: