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

More Related Content

ODP
Multithreading In Java
PPT
12 multi-threading
 
PPTX
Multithreading in java
PPT
Java Multithreading
PPTX
L22 multi-threading-introduction
PPTX
Advanced Introduction to Java Multi-Threading - Full (chok)
PPTX
MULTI THREADING IN JAVA
PPT
Thread model in java
Multithreading In Java
12 multi-threading
 
Multithreading in java
Java Multithreading
L22 multi-threading-introduction
Advanced Introduction to Java Multi-Threading - Full (chok)
MULTI THREADING IN JAVA
Thread model in java

What's hot (20)

PPT
Developing Multithreaded Applications
PPT
Basic of Multithreading in JAva
PPT
Java And Multithreading
PPSX
Multithreading in-java
PPTX
Multi threading
PPTX
Multithread Programing in Java
PPTX
Thread model of java
PDF
Java threading
ODP
Multithreading Concepts
PPT
Learning Java 3 – Threads and Synchronization
PDF
Life cycle-of-a-thread
PPT
Synchronization.37
PDF
Java threads
PPT
Java thread
PDF
Java unit 12
PDF
javathreads
PPTX
Java Thread & Multithreading
PDF
Java Thread Synchronization
PPTX
Multithreading in java
PPT
Thread
Developing Multithreaded Applications
Basic of Multithreading in JAva
Java And Multithreading
Multithreading in-java
Multi threading
Multithread Programing in Java
Thread model of java
Java threading
Multithreading Concepts
Learning Java 3 – Threads and Synchronization
Life cycle-of-a-thread
Synchronization.37
Java threads
Java thread
Java unit 12
javathreads
Java Thread & Multithreading
Java Thread Synchronization
Multithreading in java
Thread
Ad

Similar to Threading concepts (20)

PPT
multhi threading concept in oops through java
PPTX
Multithreading in Java Object Oriented Programming language
PPTX
Concept of Java Multithreading-Partially.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
PPTX
Multithreading in java
PPTX
econtent thread in java.pptx
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
PPTX
OOPS object oriented programming UNIT-4.pptx
PDF
Java Threads
PPT
Md09 multithreading
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPTX
Java-Threads And Concurrency Presentation. 2024
PPTX
Java Threads And Concurrency Presentation. 2024
PPTX
Multithreadingppt.pptx
PPTX
MSBTE Computer Engineering JPR java. multi. threading.pptx
PDF
Multithreading Introduction and Lifecyle of thread
PPT
Chap2 2 1
PPTX
Multithreading.pptx
multhi threading concept in oops through java
Multithreading in Java Object Oriented Programming language
Concept of Java Multithreading-Partially.pptx
unit3 Exception Handling multithreadingppt.pptx
unit3multithreadingppt-copy-180122162204.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
Multithreading in java
econtent thread in java.pptx
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
OOPS object oriented programming UNIT-4.pptx
Java Threads
Md09 multithreading
multithreading,thread and processinjava-210302183809.pptx
Java-Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
Multithreadingppt.pptx
MSBTE Computer Engineering JPR java. multi. threading.pptx
Multithreading Introduction and Lifecyle of thread
Chap2 2 1
Multithreading.pptx
Ad

Recently uploaded (20)

PDF
Coordination Chemistry(Part-I) - Notes.pdf
PPTX
Embark on a journey of cell division and it's stages
PDF
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
PDF
Visualizing our changing climate in real-time
PDF
NURSING FOUNDATION LESSON PLAN ON PATIENT EDUCATION.pdf
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
PPTX
Introduction to biochemistry.ppt-pdf_shotrs!
PPTX
fghvqwhfugqaifbiqufbiquvbfuqvfuqyvfqvfouiqvfq
DOCX
The beginnings of Microbiology (discovery, development, scope, Identification...
PDF
Paleoseismic activity in the moon’s Taurus-Littrowvalley inferred from boulde...
PDF
urticaria-1775-rahulkalal-250606145215-0ff37bc9.pdf
PPTX
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPTX
WEEK 4-MONO HYBRID AND DIHYBRID CROSS OF GREGOR MENDEL
PPT
oscillatoria known as blue -green algae
PPTX
LESSON 3_States of Matter and Particle Arrangement and Phase Changes.pptx
PPTX
Comparative Structure of Integument in Vertebrates.pptx
PPTX
Prawn filtration system. also known by the name pokkalii cultivation
PDF
Little Red Dots As Late-stage Quasi-stars
PPTX
Modifications in RuBisCO system to enhance photosynthesis .pptx
Coordination Chemistry(Part-I) - Notes.pdf
Embark on a journey of cell division and it's stages
Even Lighter Than Lightweiht: Augmenting Type Inference with Primitive Heuris...
Visualizing our changing climate in real-time
NURSING FOUNDATION LESSON PLAN ON PATIENT EDUCATION.pdf
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
Introduction to biochemistry.ppt-pdf_shotrs!
fghvqwhfugqaifbiqufbiquvbfuqvfuqyvfqvfouiqvfq
The beginnings of Microbiology (discovery, development, scope, Identification...
Paleoseismic activity in the moon’s Taurus-Littrowvalley inferred from boulde...
urticaria-1775-rahulkalal-250606145215-0ff37bc9.pdf
GEN. BIO 1 - CELL TYPES & CELL MODIFICATIONS
Derivatives of integument scales, beaks, horns,.pptx
WEEK 4-MONO HYBRID AND DIHYBRID CROSS OF GREGOR MENDEL
oscillatoria known as blue -green algae
LESSON 3_States of Matter and Particle Arrangement and Phase Changes.pptx
Comparative Structure of Integument in Vertebrates.pptx
Prawn filtration system. also known by the name pokkalii cultivation
Little Red Dots As Late-stage Quasi-stars
Modifications in RuBisCO system to enhance photosynthesis .pptx

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