SlideShare a Scribd company logo
1 of 13
Multithreading in Java
Multitasking - Multithreading
Hello Class

Handling multiple tasks – Multitasking
Ability to initiate multiple processes - Multithreading
Process & Threads
Process

Threads
Spell Checker in word can be considered as a thread
An application running is called a process, a process can have multiple threads.
Example - Thread

NFS

ROAD RASH

Games are best example for applying Multithreading
What are Threads?
• Two or more tasks executing concurrently within a single
•
•
•
•

•

program.
It is an independent path of execution in a program.
You always have thread in your application even if you don’t
create it.
main method is a thread and also known as main thread.
Threads are scheduled by OS to be processed by the
Processor, so its behavior depends on the OS. It cannot be
guaranteed about threads execution.
In java it is created & controlled by java.lang.Thread class
Creating Threads
Thread t = new Thread();

Thread

Just creates Thread Object

t.start();

public void run(){
// starting point for thread
}

-To start the thread start method should be invoked
-No guarantee about when it will start, depends on OS scheduler
Starting up with Threads
• There are two ways to create thread in java;
• By Extending the Thread class (java.lang.Thread)
• instance of a class that inherits from thread

• Implement the Runnable interface (java.lang.Runnable)
• instance of thread where you pass the behavior of the thread into the

thread constructor

• Let us see examples
Lifecycle of a Thread
Thread - sleep()
• A method used to tell the current thread to sleep for certain
•
•
•
•

time.
Sleep method accepts time in milliseconds.
Can throw InterruptedException.
Cannot guarantee that a thread goes to sleep for specified
time.
Once sleep state is complete, the thread can move to
Runnable or Running state.
Action Listener
Action Listener Example
• When you create a button on a Frame, and when you press it

and something happens that is called ActionListener. We tell
the button to do something when it is clicked. Or it can be
performed on TextBox, CheckBox, Radio Buttons, etc.
ActionListener
• Action Listeners are the event handlers to be implemented,

that you decide what to should be done or performed on
certain user operations. The event occurs whenever an
action is performed by user.
• Example 1
• Example 2
Done!

More Related Content

What's hot

Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
jaimefrozr
 

What's hot (20)

MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
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
 
Features of java
Features of javaFeatures of java
Features of java
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
javathreads
javathreadsjavathreads
javathreads
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
Java threads
Java threadsJava threads
Java threads
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
OOP java
OOP javaOOP java
OOP java
 
Introduction to Java Programming Language
Introduction to Java Programming LanguageIntroduction to Java Programming Language
Introduction to Java Programming Language
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Enumeration in Java Explained | Java Tutorial | Edureka
Enumeration in Java Explained | Java Tutorial | EdurekaEnumeration in Java Explained | Java Tutorial | Edureka
Enumeration in Java Explained | Java Tutorial | Edureka
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Interface in java
Interface in javaInterface in java
Interface in java
 

Viewers also liked

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
Sonali Chauhan
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
Ralf Laemmel
 
Usage concurrence in java
Usage concurrence in javaUsage concurrence in java
Usage concurrence in java
Asya Dudnik
 

Viewers also liked (20)

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Threads
ThreadsThreads
Threads
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Ch5: Threads (Operating System)
Ch5: Threads (Operating System)Ch5: Threads (Operating System)
Ch5: Threads (Operating System)
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programmingOperating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
 
Processes and threads
Processes and threadsProcesses and threads
Processes and threads
 
Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)Multithreaded programming (as part of the the PTT lecture)
Multithreaded programming (as part of the the PTT lecture)
 
Thread
ThreadThread
Thread
 
Thread lifecycle
Thread lifecycleThread lifecycle
Thread lifecycle
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Usage concurrence in java
Usage concurrence in javaUsage concurrence in java
Usage concurrence in java
 
Java threads - part 3
Java threads - part 3Java threads - part 3
Java threads - part 3
 
Java threads - part 2
Java threads - part 2Java threads - part 2
Java threads - part 2
 
Java threads - part 1
Java threads - part 1Java threads - part 1
Java threads - part 1
 
Main Memory
Main MemoryMain Memory
Main Memory
 
Java 8. Thread pools
Java 8. Thread poolsJava 8. Thread pools
Java 8. Thread pools
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
OS - Thread
OS - ThreadOS - Thread
OS - Thread
 

Similar to Thread presentation

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
 

Similar to Thread presentation (20)

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
 
Thread
ThreadThread
Thread
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java multithreading
Java multithreadingJava multithreading
Java multithreading
 
Java
JavaJava
Java
 
multithreading
multithreadingmultithreading
multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java
JavaJava
Java
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 

More from AAshish Ojha (7)

Bad UI design Example
Bad UI design ExampleBad UI design Example
Bad UI design Example
 
School management system
School management systemSchool management system
School management system
 
Library management system
Library management systemLibrary management system
Library management system
 
Customer management
Customer managementCustomer management
Customer management
 
Airline solution system proposal
Airline solution system   proposalAirline solution system   proposal
Airline solution system proposal
 
Pc remote
Pc remotePc remote
Pc remote
 
process&models in UML
process&models in UMLprocess&models in UML
process&models in UML
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Thread presentation

  • 2. Multitasking - Multithreading Hello Class Handling multiple tasks – Multitasking Ability to initiate multiple processes - Multithreading
  • 3. Process & Threads Process Threads Spell Checker in word can be considered as a thread An application running is called a process, a process can have multiple threads.
  • 4. Example - Thread NFS ROAD RASH Games are best example for applying Multithreading
  • 5. What are Threads? • Two or more tasks executing concurrently within a single • • • • • program. It is an independent path of execution in a program. You always have thread in your application even if you don’t create it. main method is a thread and also known as main thread. Threads are scheduled by OS to be processed by the Processor, so its behavior depends on the OS. It cannot be guaranteed about threads execution. In java it is created & controlled by java.lang.Thread class
  • 6. Creating Threads Thread t = new Thread(); Thread Just creates Thread Object t.start(); public void run(){ // starting point for thread } -To start the thread start method should be invoked -No guarantee about when it will start, depends on OS scheduler
  • 7. Starting up with Threads • There are two ways to create thread in java; • By Extending the Thread class (java.lang.Thread) • instance of a class that inherits from thread • Implement the Runnable interface (java.lang.Runnable) • instance of thread where you pass the behavior of the thread into the thread constructor • Let us see examples
  • 8. Lifecycle of a Thread
  • 9. Thread - sleep() • A method used to tell the current thread to sleep for certain • • • • time. Sleep method accepts time in milliseconds. Can throw InterruptedException. Cannot guarantee that a thread goes to sleep for specified time. Once sleep state is complete, the thread can move to Runnable or Running state.
  • 11. Action Listener Example • When you create a button on a Frame, and when you press it and something happens that is called ActionListener. We tell the button to do something when it is clicked. Or it can be performed on TextBox, CheckBox, Radio Buttons, etc.
  • 12. ActionListener • Action Listeners are the event handlers to be implemented, that you decide what to should be done or performed on certain user operations. The event occurs whenever an action is performed by user. • Example 1 • Example 2
  • 13. Done!