SlideShare a Scribd company logo
4
Most read
6
Most read
15
Most read
Multithreading Programming
 Multithreading is conceptual programming where a
program (processes) are divided into two or more
subprograms (processes).
 A thread is similar to program that has a single flow of
control
 It has beginning , body , an end and executes command
Sequentially
 Every program will have at least one thread.
1 RAJESHREE KHANDE
What are Threads?
 A piece of code that run in concurrent with other
threads.
 Each thread is a statically ordered sequence of
instructions.
 Threads are being extensively used express
concurrency on both single and multiprocessors
machines.
2 RAJESHREE KHANDE
A single threaded program
class ABC
{
….
public void main(..)
{
…
..
}
}
begin
body
end
3 RAJESHREE KHANDE
A Multithreaded Program
Main Thread
Thread A Thread B Thread C
start start
start
Threads may switch or exchange data/results
Main Method
Module
SwitchingSwitching
4 RAJESHREE KHANDE
Printing Thread
Editing Thread
Modern Applications need Threads (ex1):
Editing and Printing documents in background.
5 RAJESHREE KHANDE
Thread Class
 Multithreading System built upon Thread class it’s method,
it’s interface Runnable.
 To create a new Thread , either extends Thread or
implement the Runnable interface
 Thread class defines several Methods. Some of the method
are
1) getName() : Obtain a thread Name.
6 RAJESHREE KHANDE
Thread Class
2) getPriority() : Obtain a Thread Priority
3) isAlive() : Determine if the thread is still running
4) join() : Wait for thread to terminate.
5) run() : Entry point for the thread.
6) sleep() : Suspend a thread for a period of time.
7) start() : Start a thread by calling it’s run()
method
7 RAJESHREE KHANDE
Creating Thread
 Java threads may be created by:
1. Extending Thread class
2. Implementing the Runnable interface
 Java threads are managed by the JVM.
8 RAJESHREE KHANDE
1. Extending Thread class
 Declare a class as extending the Thread class
 Create instance of that class
 This class must override the run() method which is
entry point for the new thread.
 It must also call start() to begin the execution of new
thread.
9 RAJESHREE KHANDE
Extending Thread class
 Syntax
class MyThread extends Thread
{
public void run()
{
// thread body of execution
}
}
10 RAJESHREE KHANDE
 Create a thread:
MyThread thr1 = new MyThread();
 Start Execution of threads:
thr1.start();
 Create and Execute:
new MyThread().start();
1.Extending Thread class
11 RAJESHREE KHANDE
The Main Thread
 When Java Program start up one thread begins
immediately called Main Thread.
 It can be control through a object Thread
 For this obtain a reference to it by calling the method
currentThread() which is public static member of Thread
 It’s General Form
static Thread CurrentThread()
12 RAJESHREE KHANDE
Life Cycle of Thread
13
new
runnable non-runnable
dead
wait()
sleep()
suspend()
blocked
notify()
slept
resume()
unblocked
start()
stop()
RAJESHREE KHANDE
2: Threads by implementing Runnable interface
class MyThread implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
 Creating Object:
MyThread myObject = new MyThread();
 Creating Thread Object:
Thread thr1 = new Thread( myObject );
 Start Execution:
thr1.start();
14 RAJESHREE KHANDE
An example
15
class MyThread implements Runnable
{
public void run()
{
System.out.println(" this thread is running ... ");
}
} // end class MyThread
class ThreadEx2
{
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
// due to implementing the Runnable interface
// I can call start(), and this will call run().
t.start();
} // end main()
} // end class ThreadEx2
RAJESHREE KHANDE

More Related Content

What's hot (20)

PPTX
Packages in java
Elizabeth alexander
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PPTX
Data types in java
HarshitaAshwani
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Identifier
ASHUTOSH TRIVEDI
 
PPTX
I/O Streams
Ravi Chythanya
 
PDF
Remote Method Invocation (RMI)
Peter R. Egli
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Control statements in java
Madishetty Prathibha
 
PPTX
java interface and packages
VINOTH R
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Storage class in C Language
Nitesh Kumar Pandey
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
OOPS In JAVA.pptx
Sachin33417
 
PPT
Java access modifiers
Srinivas Reddy
 
PPTX
Inheritance in java
Tech_MX
 
PPT
Java adapter
Arati Gadgil
 
Packages in java
Elizabeth alexander
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Data types in java
HarshitaAshwani
 
Java threads
Prabhakaran V M
 
Identifier
ASHUTOSH TRIVEDI
 
I/O Streams
Ravi Chythanya
 
Remote Method Invocation (RMI)
Peter R. Egli
 
Classes objects in java
Madishetty Prathibha
 
Control statements in java
Madishetty Prathibha
 
java interface and packages
VINOTH R
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Storage class in C Language
Nitesh Kumar Pandey
 
Interface in java
PhD Research Scholar
 
Java Data Types
Spotle.ai
 
OOPS In JAVA.pptx
Sachin33417
 
Java access modifiers
Srinivas Reddy
 
Inheritance in java
Tech_MX
 
Java adapter
Arati Gadgil
 

Similar to Java Multi-threading programming (20)

PPTX
Lecture 23-24.pptx
talha ijaz
 
PPT
multithreading, creating a thread and life cycle in java.ppt
shikhaverma566116
 
PPT
9.multi-threading latest(MB).ppt .
happycocoman
 
PPTX
Multithreading programming in java
Elizabeth alexander
 
PPT
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
PPTX
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
PPTX
Multithreading in java
Kavitha713564
 
PPTX
Multithreading in java
Kavitha713564
 
PPTX
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
PDF
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
kassyemariyam21
 
PDF
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
DOCX
Threadnotes
Himanshu Rajput
 
PPTX
Multithreading.pptx
ssuserfcae42
 
PPT
BCA MultiThreading.ppt
sarthakgithub
 
PPTX
Multithreading.pptx
PragatiSutar4
 
DOCX
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
PPTX
L22 multi-threading-introduction
teach4uin
 
PDF
Multi threading
Ravi Kant Sahu
 
PPTX
Internet Programming with Java
kavitha muneeshwaran
 
Lecture 23-24.pptx
talha ijaz
 
multithreading, creating a thread and life cycle in java.ppt
shikhaverma566116
 
9.multi-threading latest(MB).ppt .
happycocoman
 
Multithreading programming in java
Elizabeth alexander
 
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
Multithreading in java
Kavitha713564
 
Multithreading in java
Kavitha713564
 
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
kassyemariyam21
 
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Threadnotes
Himanshu Rajput
 
Multithreading.pptx
ssuserfcae42
 
BCA MultiThreading.ppt
sarthakgithub
 
Multithreading.pptx
PragatiSutar4
 
Module - 5 merged.docx notes about engineering subjects java
KaviShetty
 
L22 multi-threading-introduction
teach4uin
 
Multi threading
Ravi Kant Sahu
 
Internet Programming with Java
kavitha muneeshwaran
 
Ad

More from DrRajeshreeKhande (20)

PPTX
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
PPTX
Exception Handling in .NET F#
DrRajeshreeKhande
 
PPTX
.NET F# Events
DrRajeshreeKhande
 
PPTX
.NET F# Class constructor
DrRajeshreeKhande
 
PPTX
.NET F# Abstract class interface
DrRajeshreeKhande
 
PPTX
.Net F# Generic class
DrRajeshreeKhande
 
PPTX
F# Console class
DrRajeshreeKhande
 
PPTX
.net F# mutable dictionay
DrRajeshreeKhande
 
PPTX
F sharp lists & dictionary
DrRajeshreeKhande
 
PPTX
F# array searching
DrRajeshreeKhande
 
PPTX
Net (f#) array
DrRajeshreeKhande
 
PPTX
.Net (F # ) Records, lists
DrRajeshreeKhande
 
PPT
MS Office for Beginners
DrRajeshreeKhande
 
PPSX
Java String class
DrRajeshreeKhande
 
PPTX
JAVA AWT components
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande Java Interactive input
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande :Intoduction to java
DrRajeshreeKhande
 
PPSX
Java Exceptions Handling
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
 
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
Exception Handling in .NET F#
DrRajeshreeKhande
 
.NET F# Events
DrRajeshreeKhande
 
.NET F# Class constructor
DrRajeshreeKhande
 
.NET F# Abstract class interface
DrRajeshreeKhande
 
.Net F# Generic class
DrRajeshreeKhande
 
F# Console class
DrRajeshreeKhande
 
.net F# mutable dictionay
DrRajeshreeKhande
 
F sharp lists & dictionary
DrRajeshreeKhande
 
F# array searching
DrRajeshreeKhande
 
Net (f#) array
DrRajeshreeKhande
 
.Net (F # ) Records, lists
DrRajeshreeKhande
 
MS Office for Beginners
DrRajeshreeKhande
 
Java String class
DrRajeshreeKhande
 
JAVA AWT components
DrRajeshreeKhande
 
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
Dr. Rajeshree Khande Java Interactive input
DrRajeshreeKhande
 
Dr. Rajeshree Khande :Intoduction to java
DrRajeshreeKhande
 
Java Exceptions Handling
DrRajeshreeKhande
 
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
 
Ad

Recently uploaded (20)

PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 

Java Multi-threading programming

  • 1. Multithreading Programming  Multithreading is conceptual programming where a program (processes) are divided into two or more subprograms (processes).  A thread is similar to program that has a single flow of control  It has beginning , body , an end and executes command Sequentially  Every program will have at least one thread. 1 RAJESHREE KHANDE
  • 2. What are Threads?  A piece of code that run in concurrent with other threads.  Each thread is a statically ordered sequence of instructions.  Threads are being extensively used express concurrency on both single and multiprocessors machines. 2 RAJESHREE KHANDE
  • 3. A single threaded program class ABC { …. public void main(..) { … .. } } begin body end 3 RAJESHREE KHANDE
  • 4. A Multithreaded Program Main Thread Thread A Thread B Thread C start start start Threads may switch or exchange data/results Main Method Module SwitchingSwitching 4 RAJESHREE KHANDE
  • 5. Printing Thread Editing Thread Modern Applications need Threads (ex1): Editing and Printing documents in background. 5 RAJESHREE KHANDE
  • 6. Thread Class  Multithreading System built upon Thread class it’s method, it’s interface Runnable.  To create a new Thread , either extends Thread or implement the Runnable interface  Thread class defines several Methods. Some of the method are 1) getName() : Obtain a thread Name. 6 RAJESHREE KHANDE
  • 7. Thread Class 2) getPriority() : Obtain a Thread Priority 3) isAlive() : Determine if the thread is still running 4) join() : Wait for thread to terminate. 5) run() : Entry point for the thread. 6) sleep() : Suspend a thread for a period of time. 7) start() : Start a thread by calling it’s run() method 7 RAJESHREE KHANDE
  • 8. Creating Thread  Java threads may be created by: 1. Extending Thread class 2. Implementing the Runnable interface  Java threads are managed by the JVM. 8 RAJESHREE KHANDE
  • 9. 1. Extending Thread class  Declare a class as extending the Thread class  Create instance of that class  This class must override the run() method which is entry point for the new thread.  It must also call start() to begin the execution of new thread. 9 RAJESHREE KHANDE
  • 10. Extending Thread class  Syntax class MyThread extends Thread { public void run() { // thread body of execution } } 10 RAJESHREE KHANDE
  • 11.  Create a thread: MyThread thr1 = new MyThread();  Start Execution of threads: thr1.start();  Create and Execute: new MyThread().start(); 1.Extending Thread class 11 RAJESHREE KHANDE
  • 12. The Main Thread  When Java Program start up one thread begins immediately called Main Thread.  It can be control through a object Thread  For this obtain a reference to it by calling the method currentThread() which is public static member of Thread  It’s General Form static Thread CurrentThread() 12 RAJESHREE KHANDE
  • 13. Life Cycle of Thread 13 new runnable non-runnable dead wait() sleep() suspend() blocked notify() slept resume() unblocked start() stop() RAJESHREE KHANDE
  • 14. 2: Threads by implementing Runnable interface class MyThread implements Runnable { ..... public void run() { // thread body of execution } }  Creating Object: MyThread myObject = new MyThread();  Creating Thread Object: Thread thr1 = new Thread( myObject );  Start Execution: thr1.start(); 14 RAJESHREE KHANDE
  • 15. An example 15 class MyThread implements Runnable { public void run() { System.out.println(" this thread is running ... "); } } // end class MyThread class ThreadEx2 { public static void main(String [] args ) { Thread t = new Thread(new MyThread()); // due to implementing the Runnable interface // I can call start(), and this will call run(). t.start(); } // end main() } // end class ThreadEx2 RAJESHREE KHANDE