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

Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
myrajendra
 

What's hot (20)

Thread
ThreadThread
Thread
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Java threading
Java threadingJava threading
Java threading
 
Thread priority in java
Thread priority in javaThread priority in java
Thread priority in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java threads
Java threadsJava threads
Java threads
 
Java-java virtual machine
Java-java virtual machineJava-java virtual machine
Java-java virtual machine
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Servlet
Servlet Servlet
Servlet
 
Java Multi Thead Programming
Java Multi Thead ProgrammingJava Multi Thead Programming
Java Multi Thead Programming
 
OOP java
OOP javaOOP java
OOP java
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
11. java methods
11. java methods11. java methods
11. java methods
 
Basics of Java Concurrency
Basics of Java ConcurrencyBasics of Java Concurrency
Basics of Java Concurrency
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Arrays in Java
Arrays in Java Arrays in Java
Arrays in Java
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 

Viewers also liked

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
Sonali Chauhan
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
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 (19)

Operating System-Threads-Galvin
Operating System-Threads-GalvinOperating System-Threads-Galvin
Operating System-Threads-Galvin
 
Threads
ThreadsThreads
Threads
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading 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 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
 
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
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 

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
 
Lecture 23-24.pptx
Lecture 23-24.pptxLecture 23-24.pptx
Lecture 23-24.pptx
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
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
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 

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

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
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
 
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 setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
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)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.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...
 
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
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

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!