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

More Related Content

What's hot

Developing Multithreaded Applications
Developing Multithreaded ApplicationsDeveloping Multithreaded Applications
Developing Multithreaded ApplicationsBharat17485
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAvasuraj pandey
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-javaaalipalh
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in JavaM. Raihan
 
Thread model of java
Thread model of javaThread model of java
Thread model of javamyrajendra
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronizationcaswenson
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37myrajendra
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreadingjehan1987
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javajunnubabu
 

What's hot (20)

Developing Multithreaded Applications
Developing Multithreaded ApplicationsDeveloping Multithreaded Applications
Developing Multithreaded Applications
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Java threading
Java threadingJava threading
Java threading
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
 
Java threads
Java threadsJava threads
Java threads
 
Java thread
Java threadJava thread
Java thread
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
javathreads
javathreadsjavathreads
javathreads
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Thread
ThreadThread
Thread
 

Similar to Threading concepts

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 languagearnavytstudio2814
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptxnimbalkarvikram966
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptxramyan49
 
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.pptxArulmozhivarman8
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024nehakumari0xf
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024kashyapneha2809
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptxHKShab
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
Java class 6
Java class 6Java class 6
Java class 6Edureka!
 
JAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionJAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionSaikiranBiradar3
 

Similar to Threading concepts (20)

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
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
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
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Lecture 20
Lecture 20Lecture 20
Lecture 20
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Java class 6
Java class 6Java class 6
Java class 6
 
JAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionJAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transaction
 

Recently uploaded

LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxmalonesandreagweneth
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxEran Akiva Sinbar
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPirithiRaju
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPirithiRaju
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantadityabhardwaj282
 
Pests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsHajira Mahmood
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...lizamodels9
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensorsonawaneprad
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trssuser06f238
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxBerniceCayabyab1
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)DHURKADEVIBASKAR
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPirithiRaju
 
OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024innovationoecd
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxEran Akiva Sinbar
 
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRlizamodels9
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentationtahreemzahra82
 
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxBREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxPABOLU TEJASREE
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 

Recently uploaded (20)

LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptxLIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
LIGHT-PHENOMENA-BY-CABUALDIONALDOPANOGANCADIENTE-CONDEZA (1).pptx
 
The dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptxThe dark energy paradox leads to a new structure of spacetime.pptx
The dark energy paradox leads to a new structure of spacetime.pptx
 
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdfPests of soyabean_Binomics_IdentificationDr.UPR.pdf
Pests of soyabean_Binomics_IdentificationDr.UPR.pdf
 
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdfPests of jatropha_Bionomics_identification_Dr.UPR.pdf
Pests of jatropha_Bionomics_identification_Dr.UPR.pdf
 
Forest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are importantForest laws, Indian forest laws, why they are important
Forest laws, Indian forest laws, why they are important
 
Pests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdfPests of castor_Binomics_Identification_Dr.UPR.pdf
Pests of castor_Binomics_Identification_Dr.UPR.pdf
 
Solution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutionsSolution chemistry, Moral and Normal solutions
Solution chemistry, Moral and Normal solutions
 
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
Best Call Girls In Sector 29 Gurgaon❤️8860477959 EscorTs Service In 24/7 Delh...
 
Environmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial BiosensorEnvironmental Biotechnology Topic:- Microbial Biosensor
Environmental Biotechnology Topic:- Microbial Biosensor
 
Neurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 trNeurodevelopmental disorders according to the dsm 5 tr
Neurodevelopmental disorders according to the dsm 5 tr
 
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptxGenBio2 - Lesson 1 - Introduction to Genetics.pptx
GenBio2 - Lesson 1 - Introduction to Genetics.pptx
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)Recombinant DNA technology( Transgenic plant and animal)
Recombinant DNA technology( Transgenic plant and animal)
 
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdfPests of safflower_Binomics_Identification_Dr.UPR.pdf
Pests of safflower_Binomics_Identification_Dr.UPR.pdf
 
OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024OECD bibliometric indicators: Selected highlights, April 2024
OECD bibliometric indicators: Selected highlights, April 2024
 
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptxTwin's paradox experiment is a meassurement of the extra dimensions.pptx
Twin's paradox experiment is a meassurement of the extra dimensions.pptx
 
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCRCall Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
Call Girls In Nihal Vihar Delhi ❤️8860477959 Looking Escorts In 24/7 Delhi NCR
 
Harmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms PresentationHarmful and Useful Microorganisms Presentation
Harmful and Useful Microorganisms Presentation
 
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptxBREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
BREEDING FOR RESISTANCE TO BIOTIC STRESS.pptx
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 

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