SlideShare a Scribd company logo
CMP-2123-Object Oriented
Programming
Lecture 10
By
Abrar Ahmad
Abrar.ahmad14@ce.ceme.edu.pk
Java - Multithreading
Badar Waseer arbabwaseergmail.com 2
• Java is a multi-threaded programming language which means we can
develop multi-threaded program using Java. A multi-threaded
program contains two or more parts that can run concurrently and
each part can handle a different task at the same time making
optimal use of the available resources specially when your computer
has multiple CPUs.
• By definition, multitasking is when multiple processes share common
processing resources such as a CPU. Multi-threading extends the idea
of multitasking into applications where you can subdivide specific
operations within a single application into individual threads. Each of
the threads can run in parallel. The OS divides processing time not
only among different applications, but also among each thread within
an application.
• Multi-threading enables you to write in a way where multiple
activities can proceed concurrently in the same program.
Badar Waseer arbabwaseergmail.com 3
Life Cycle of a Thread
• A thread goes through various stages in its life cycle. For example, a
thread is born, started, runs, and then dies. The following diagram
shows the complete life cycle of a thread
Badar Waseer arbabwaseergmail.com 4
Badar Waseer arbabwaseergmail.com 5
• Following are the stages of the life cycle −
• New − A new thread begins its life cycle in the new state. It remains in this state
until the program starts the thread. It is also referred to as a born thread.
• Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
• Waiting − Sometimes, a thread transitions to the waiting state while the thread
waits for another thread to perform a task. A thread transitions back to the
runnable state only when another thread signals the waiting thread to continue
executing.
• Timed Waiting − A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transitions back to the runnable
state when that time interval expires or when the event it is waiting for occurs.
• Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.
Badar Waseer arbabwaseergmail.com 6
Thread Priorities
• Every Java thread has a priority that helps the operating system
determine the order in which threads are scheduled.
• Java thread priorities are in the range between MIN_PRIORITY (a
constant of 1) and MAX_PRIORITY (a constant of 10). By default, every
thread is given priority NORM_PRIORITY (a constant of 5).
• Threads with higher priority are more important to a program and
should be allocated processor time before lower-priority threads.
However, thread priorities cannot guarantee the order in which
threads execute and are very much platform dependent.
Badar Waseer arbabwaseergmail.com 7
Create a Thread by Implementing a Runnable
Interface
• If your class is intended to be executed as a thread then you can
achieve this by implementing a Runnable interface. You will need to
follow three basic steps −
Badar Waseer arbabwaseergmail.com 8
• Step 1
• As a first step, you need to implement a run() method provided by
a Runnable interface. This method provides an entry point for the
thread and you will put your complete business logic inside this
method. Following is a simple syntax of the run() method
Badar Waseer arbabwaseergmail.com 9
• Step 2
• As a second step, you will instantiate a Thread object using the
following constructor −
• Where, threadObj is an instance of a class that implements
the Runnable interface and threadName is the name given to the
new thread.
Badar Waseer arbabwaseergmail.com 10
• Step 3
• Once a Thread object is created, you can start it by
calling start() method, which executes a call to run( ) method.
Following is a simple syntax of start() method −
Badar Waseer arbabwaseergmail.com 11
Badar Waseer arbabwaseergmail.com 12
Badar Waseer arbabwaseergmail.com 13
Any Questions ?
Badar Waseer arbabwaseergmail.com 14
Thank You
Badar Waseer arbabwaseergmail.com 15

More Related Content

What's hot (9)

PPTX
Inheritance1
Daman Toor
 
PPTX
PROGRAMMING IN JAVA- unit 4-part I
SivaSankari36
 
PPTX
PROGRAMMING IN JAVA- unit 5-part II
SivaSankari36
 
PPTX
Basic syntax
Ducat India
 
PPTX
Mule with stored procedure
Anirban Sen Chowdhary
 
PDF
Introducing Ruby/MVC/RoR
Sumanth krishna
 
PPTX
Enterprise java unit-2_chapter-1
sandeep54552
 
PPTX
Database integrate with mule
Son Nguyen
 
PPTX
Jdbc
Yamuna Devi
 
Inheritance1
Daman Toor
 
PROGRAMMING IN JAVA- unit 4-part I
SivaSankari36
 
PROGRAMMING IN JAVA- unit 5-part II
SivaSankari36
 
Basic syntax
Ducat India
 
Mule with stored procedure
Anirban Sen Chowdhary
 
Introducing Ruby/MVC/RoR
Sumanth krishna
 
Enterprise java unit-2_chapter-1
sandeep54552
 
Database integrate with mule
Son Nguyen
 

Similar to Lec 1.10 Object Oriented Programming (20)

PPTX
Lecture 23-24.pptx
talha ijaz
 
PDF
Java thread life cycle
Archana Gopinath
 
PPTX
Internet Programming with Java
kavitha muneeshwaran
 
PPTX
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
PPTX
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
PPTX
Multi threading
gndu
 
DOCX
Threadnotes
Himanshu Rajput
 
PPTX
MULTI THREADING IN JAVA
VINOTH R
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Multithreading in java
Monika Mishra
 
PPT
BCA MultiThreading.ppt
sarthakgithub
 
PPTX
Multithreading in java
Kavitha713564
 
PPTX
Multithreading in java
Kavitha713564
 
PPTX
Multithreading.pptx
PragatiSutar4
 
PPTX
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
PDF
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
PPT
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
PPTX
Thread
Mohamed Essam
 
PPTX
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Lecture 23-24.pptx
talha ijaz
 
Java thread life cycle
Archana Gopinath
 
Internet Programming with Java
kavitha muneeshwaran
 
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
Multi threading
gndu
 
Threadnotes
Himanshu Rajput
 
MULTI THREADING IN JAVA
VINOTH R
 
Java threads
Prabhakaran V M
 
Multithreading in java
Monika Mishra
 
BCA MultiThreading.ppt
sarthakgithub
 
Multithreading in java
Kavitha713564
 
Multithreading in java
Kavitha713564
 
Multithreading.pptx
PragatiSutar4
 
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Ad

More from Badar Waseer (20)

PDF
Ch 03 s.e agile development
Badar Waseer
 
DOCX
Ch 02 s.e software process models 1
Badar Waseer
 
DOCX
Ch 01 s.e introduction
Badar Waseer
 
PPTX
Ch 14 s.e use case diagrams
Badar Waseer
 
PPT
Ch 13 s.e cmmi
Badar Waseer
 
PPTX
Lec 1.9 oop
Badar Waseer
 
PPTX
Lec 1.8 Object Oriented Programming
Badar Waseer
 
PPTX
Lec 1.7 Object Oriented Programming
Badar Waseer
 
PPTX
Lec 1.5 Object Oriented Programming
Badar Waseer
 
PPTX
Lec 1.4 Object Oriented Programming
Badar Waseer
 
PPTX
Lec 1.3 Object Oriented Programming
Badar Waseer
 
PPTX
Lec 1.1 Object Oriented Programming
Badar Waseer
 
PPTX
Multimedia System & Design Ch 7 multimedia skills
Badar Waseer
 
PPTX
Multimedia System & Design Ch 6 animation
Badar Waseer
 
PPTX
Multimedia System & Design Ch 5 video
Badar Waseer
 
PPTX
Multimedia System & Design Ch 4 Audio
Badar Waseer
 
PPTX
Multimedia System & Design Ch 1, 2, 3 Multimedia
Badar Waseer
 
PPTX
Multimedia System & Design Ch 8 delivering
Badar Waseer
 
PPTX
Agriculture Information System (AIS)
Badar Waseer
 
PDF
Output Devices UoS Fsd
Badar Waseer
 
Ch 03 s.e agile development
Badar Waseer
 
Ch 02 s.e software process models 1
Badar Waseer
 
Ch 01 s.e introduction
Badar Waseer
 
Ch 14 s.e use case diagrams
Badar Waseer
 
Ch 13 s.e cmmi
Badar Waseer
 
Lec 1.9 oop
Badar Waseer
 
Lec 1.8 Object Oriented Programming
Badar Waseer
 
Lec 1.7 Object Oriented Programming
Badar Waseer
 
Lec 1.5 Object Oriented Programming
Badar Waseer
 
Lec 1.4 Object Oriented Programming
Badar Waseer
 
Lec 1.3 Object Oriented Programming
Badar Waseer
 
Lec 1.1 Object Oriented Programming
Badar Waseer
 
Multimedia System & Design Ch 7 multimedia skills
Badar Waseer
 
Multimedia System & Design Ch 6 animation
Badar Waseer
 
Multimedia System & Design Ch 5 video
Badar Waseer
 
Multimedia System & Design Ch 4 Audio
Badar Waseer
 
Multimedia System & Design Ch 1, 2, 3 Multimedia
Badar Waseer
 
Multimedia System & Design Ch 8 delivering
Badar Waseer
 
Agriculture Information System (AIS)
Badar Waseer
 
Output Devices UoS Fsd
Badar Waseer
 
Ad

Recently uploaded (20)

PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
community health nursing question paper 2.pdf
Prince kumar
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 

Lec 1.10 Object Oriented Programming

  • 2. Java - Multithreading Badar Waseer arbabwaseergmail.com 2
  • 3. • Java is a multi-threaded programming language which means we can develop multi-threaded program using Java. A multi-threaded program contains two or more parts that can run concurrently and each part can handle a different task at the same time making optimal use of the available resources specially when your computer has multiple CPUs. • By definition, multitasking is when multiple processes share common processing resources such as a CPU. Multi-threading extends the idea of multitasking into applications where you can subdivide specific operations within a single application into individual threads. Each of the threads can run in parallel. The OS divides processing time not only among different applications, but also among each thread within an application. • Multi-threading enables you to write in a way where multiple activities can proceed concurrently in the same program. Badar Waseer arbabwaseergmail.com 3
  • 4. Life Cycle of a Thread • A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. The following diagram shows the complete life cycle of a thread Badar Waseer arbabwaseergmail.com 4
  • 6. • Following are the stages of the life cycle − • New − A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. • Runnable − After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. • Waiting − Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing. • Timed Waiting − A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. • Terminated (Dead) − A runnable thread enters the terminated state when it completes its task or otherwise terminates. Badar Waseer arbabwaseergmail.com 6
  • 7. Thread Priorities • Every Java thread has a priority that helps the operating system determine the order in which threads are scheduled. • Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and MAX_PRIORITY (a constant of 10). By default, every thread is given priority NORM_PRIORITY (a constant of 5). • Threads with higher priority are more important to a program and should be allocated processor time before lower-priority threads. However, thread priorities cannot guarantee the order in which threads execute and are very much platform dependent. Badar Waseer arbabwaseergmail.com 7
  • 8. Create a Thread by Implementing a Runnable Interface • If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. You will need to follow three basic steps − Badar Waseer arbabwaseergmail.com 8
  • 9. • Step 1 • As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. Following is a simple syntax of the run() method Badar Waseer arbabwaseergmail.com 9
  • 10. • Step 2 • As a second step, you will instantiate a Thread object using the following constructor − • Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread. Badar Waseer arbabwaseergmail.com 10
  • 11. • Step 3 • Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method − Badar Waseer arbabwaseergmail.com 11
  • 14. Any Questions ? Badar Waseer arbabwaseergmail.com 14
  • 15. Thank You Badar Waseer arbabwaseergmail.com 15