SlideShare a Scribd company logo
MULTITHREADING
2.1 Understand the concept of Threading
Learning Outcomes
At the end of this presentation, you will be able to
• Define Thread and its uses of Thread in Java programs
• Explain the different types of Thread
• Differentiate between multi-tasking and multi-threading
• List the method involved in life cycle of a Thread
Thread Definition
• As a human , we can do more than one action at a time.
• In java this concept is called Thread (java.lang.Thread
class).
• A thread (urutan) is the flow of execution, from the
beginning to end, of a task in a program - processes
executed by a program
• Uses in Java program
 Web server – database
 Animation – game
Types of Thread
• Two types of Thread
– Single Thread
– Multiple Thread (multithreaded)
Single Thread
• A task or process that is made up of only one thread.
• A program that supports single thread is called single
threaded application.
Multiple Thread
• A task or process that is made of more
than one thread is multiple threads.
• A program that supports more than one
thread is multi-threaded application
Another
section/process of the
same program
Multitasking vs Multithreading
Multitasking Multithreading
Is the ability of an
operating system to
execute more than
one program
simultaneously
Is the capability of
running multiple tasks
concurrently within a
program
Difference between multi-tasking and
multi-threading
Multitasking Multithreading
More than one program gets
executed simultaneously.
More than one part of a
program called threads is
executed simultaneously.
Multitasking is a timesharing
process. CPU switches from
one program to another
program so quickly to
complete all the programs
simultaneously.
Multithreading is also a
timesharing process. CPU
switches from one activity to
another activity within the
program so quickly to complete
all the activities simultaneously.
Since each program occupies
different memory location,
Multitasking is called as
heavyweight process.
Multithreading is a light weight
process because the activities/
threads share the same memory
space.
States Description
Newborn When a thread is created.
Runnable It is ready for execution and is waiting
for the availability of CPU.
Running It is the time given for the execution of
the thread.
Blocked The thread is suspended, sleeping or
waiting to fulfil certain requirements.
Dead A thread completes executing the
run() method, ends its life.
States in a Life Cycle of a Thread
Methods Description
start() Used to initiate the thread.
stop() Used to kill the thread.
run() Used to implement the body of the thread and
its behaviour.
yield() Used to give the control to another thread of
equal priority.
suspend() Used to suspend the thread.
resume() Used to resume the suspended thread.
Methods in Thread Class
sleep(t) Used to specify the time frame for
suspension.
wait() Used to make the thread wait until some
event occurs.
notify() Used to schedule the thread to run again.
Methods in Thread Class
Thread created
Newborn
Running
Runnable
Blocked
Dead
start
run
stop
yield/time
expired
suspend, sleep / wait
stop
stop
/complete
resume/notify
Life Cycle of a Thread
 There are two ways to create a java thread:
 By extending the Thread class
(java.lang.Thread)
 By implementing the Runnable interface
(java.lang.Runnable)
Creating Threads
class FirstThread extends Thread
{
public void run()
{
int a = 8,b = 4,sum;
sum = a + b;
System.out.println("Sum is "+sum);
}
}
class ThreadDemo2{
public static void main(String args[]){
FirstThread obj1 = new FirstThread();
obj1.start();
}
}
extends Thread class
override the run() method
Use start() method to start the thread
class FirstThread implements Runnable {
public void run()
{
int a = 8,b = 4,sum;
sum = a + b;
System.out.println("Sum is "+sum);
}
}
class ThreadDemo2{
public static void main(String args[]){
FirstThread obj1 = new FirstThread();
Thread t = new Thread(obj1);
t.start();
}
}
Implements Runnable
interface
override the run() method
Use start() method to start the thread
Extending Thread vs. Implementing
Runnable Interface
• Choosing between these two is a matter of taste
• Implementing the Runnable interface
– May take more work since we still
• Declare a Thread object
• Call the Thread methods on this object
• Extending the Thread class
– Easier to implement
– Your class can no longer extend any other class
EXERCISE
• Based on the situations below, identify and state the
appropriate methods.
i. Thread will suspend according to the time
frame.
ii. This method is used to schedule the thread to run
again.
iii. Thread will wait until some event occurs.
iv. This method will resume the suspended thread.
v. Thread will stop.
Match the state with correct answer
Newborn
Thread is created.
Runnable
It is ready for execution and is waiting
for the availability of CPU.
Running
It is the time given for the execution of
the thread.
Blocked
The thread is suspended, sleeping or
waiting to fulfil certain requirements.
Dead
A thread completes executing the
run() method, ends its life.
In this presentation, you learnt the following
• Threads can be defined as single, sequential
flow of control within a program.
• The two types of threads are single and
multiple threads
• A process that is made of only one thread is
called single threaded application.
Summary
• A program that supports more than one thread
is multi-threaded application.
• The lifetime of a thread includes the Newborn,
Runnable, Running, Blocked and Dead states.

More Related Content

PPT
Java And Multithreading
PPTX
Multithread Programing in Java
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
ODP
Multithreading In Java
PPT
Basic of Multithreading in JAva
PPTX
Multithreading in java
PPTX
Multi-threaded Programming in JAVA
Java And Multithreading
Multithread Programing in Java
Abstract Class & Abstract Method in Core Java
Multithreading In Java
Basic of Multithreading in JAva
Multithreading in java
Multi-threaded Programming in JAVA

What's hot (20)

PPTX
Event handling
PPTX
JAVA AWT
PPT
Java adapter
PPTX
Multithreading in java
PDF
Java Thread Synchronization
PDF
Servlet and servlet life cycle
PDF
Remote Method Invocation in JAVA
PDF
input/ output in java
PPTX
Multithreading in java
PPT
Thread model in java
PPTX
Applets in java
PPTX
Classes, objects in JAVA
PPTX
Multiple inheritance possible in Java
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
This keyword in java
PPT
Exception handling
PPTX
Java swing
PPT
Abstract class in java
Event handling
JAVA AWT
Java adapter
Multithreading in java
Java Thread Synchronization
Servlet and servlet life cycle
Remote Method Invocation in JAVA
input/ output in java
Multithreading in java
Thread model in java
Applets in java
Classes, objects in JAVA
Multiple inheritance possible in Java
Basic Concepts of OOPs (Object Oriented Programming in Java)
This keyword in java
Exception handling
Java swing
Abstract class in java
Ad

Similar to Chap2 2 1 (20)

PPTX
Internet Programming with Java
PDF
Multithreading Introduction and Lifecyle of thread
PPTX
Multithreading in java
PPT
multhi threading concept in oops through java
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PDF
Class notes(week 9) on multithreading
PPT
multithreading, creating a thread and life cycle in java.ppt
PPTX
Multi-Threading in Java power point presenetation
PPTX
multithreading,thread and processinjava-210302183809.pptx
DOCX
Threadnotes
PPTX
Concept of Java Multithreading-Partially.pptx
PDF
Java unit 12
PPTX
Java Multithreading - how to create threads
PPTX
MULTI THREADING IN JAVA
PPTX
Threading concepts
PPTX
U4 JAVA.pptx
DOCX
Module - 5 merged.docx notes about engineering subjects java
Internet Programming with Java
Multithreading Introduction and Lifecyle of thread
Multithreading in java
multhi threading concept in oops through java
OOPS object oriented programming UNIT-4.pptx
unit3multithreadingppt-copy-180122162204.pptx
unit3 Exception Handling multithreadingppt.pptx
Class notes(week 9) on multithreading
multithreading, creating a thread and life cycle in java.ppt
Multi-Threading in Java power point presenetation
multithreading,thread and processinjava-210302183809.pptx
Threadnotes
Concept of Java Multithreading-Partially.pptx
Java unit 12
Java Multithreading - how to create threads
MULTI THREADING IN JAVA
Threading concepts
U4 JAVA.pptx
Module - 5 merged.docx notes about engineering subjects java
Ad

More from Hemo Chella (15)

PPT
Chap4 4 2
PPT
Chap4 4 1
PPT
Chap3 3 12
PPT
Chap1 1 4
PPT
Chap1 1 1
PPT
Chap1 1.4
PPT
Chap1 1.1
PPT
Fp601 chapter 6
PPT
Fp601 chapter 5 -part 1
PPT
Fp601 chapter 5 - part 2
PPT
Fp601 chapter 4
PPT
Fp601 chapter 3 - part 2
PPT
Fp601 chapter 3 - part 1
PPT
Fp601 chapter 2
PPT
Fp601 chapter 1
Chap4 4 2
Chap4 4 1
Chap3 3 12
Chap1 1 4
Chap1 1 1
Chap1 1.4
Chap1 1.1
Fp601 chapter 6
Fp601 chapter 5 -part 1
Fp601 chapter 5 - part 2
Fp601 chapter 4
Fp601 chapter 3 - part 2
Fp601 chapter 3 - part 1
Fp601 chapter 2
Fp601 chapter 1

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Lesson notes of climatology university.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
Final Presentation General Medicine 03-08-2024.pptx
Pharma ospi slides which help in ospi learning
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Basic Mud Logging Guide for educational purpose
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Module 4: Burden of Disease Tutorial Slides S2 2025
Lesson notes of climatology university.
O5-L3 Freight Transport Ops (International) V1.pdf
O7-L3 Supply Chain Operations - ICLT Program
PPH.pptx obstetrics and gynecology in nursing
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Types and Its function , kingdom of life
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis

Chap2 2 1

  • 1. MULTITHREADING 2.1 Understand the concept of Threading
  • 2. Learning Outcomes At the end of this presentation, you will be able to • Define Thread and its uses of Thread in Java programs • Explain the different types of Thread • Differentiate between multi-tasking and multi-threading • List the method involved in life cycle of a Thread
  • 3. Thread Definition • As a human , we can do more than one action at a time. • In java this concept is called Thread (java.lang.Thread class). • A thread (urutan) is the flow of execution, from the beginning to end, of a task in a program - processes executed by a program • Uses in Java program  Web server – database  Animation – game
  • 4. Types of Thread • Two types of Thread – Single Thread – Multiple Thread (multithreaded)
  • 5. Single Thread • A task or process that is made up of only one thread. • A program that supports single thread is called single threaded application.
  • 6. Multiple Thread • A task or process that is made of more than one thread is multiple threads. • A program that supports more than one thread is multi-threaded application Another section/process of the same program
  • 7. Multitasking vs Multithreading Multitasking Multithreading Is the ability of an operating system to execute more than one program simultaneously Is the capability of running multiple tasks concurrently within a program
  • 8. Difference between multi-tasking and multi-threading Multitasking Multithreading More than one program gets executed simultaneously. More than one part of a program called threads is executed simultaneously. Multitasking is a timesharing process. CPU switches from one program to another program so quickly to complete all the programs simultaneously. Multithreading is also a timesharing process. CPU switches from one activity to another activity within the program so quickly to complete all the activities simultaneously. Since each program occupies different memory location, Multitasking is called as heavyweight process. Multithreading is a light weight process because the activities/ threads share the same memory space.
  • 9. States Description Newborn When a thread is created. Runnable It is ready for execution and is waiting for the availability of CPU. Running It is the time given for the execution of the thread. Blocked The thread is suspended, sleeping or waiting to fulfil certain requirements. Dead A thread completes executing the run() method, ends its life. States in a Life Cycle of a Thread
  • 10. Methods Description start() Used to initiate the thread. stop() Used to kill the thread. run() Used to implement the body of the thread and its behaviour. yield() Used to give the control to another thread of equal priority. suspend() Used to suspend the thread. resume() Used to resume the suspended thread. Methods in Thread Class
  • 11. sleep(t) Used to specify the time frame for suspension. wait() Used to make the thread wait until some event occurs. notify() Used to schedule the thread to run again. Methods in Thread Class
  • 12. Thread created Newborn Running Runnable Blocked Dead start run stop yield/time expired suspend, sleep / wait stop stop /complete resume/notify Life Cycle of a Thread
  • 13.  There are two ways to create a java thread:  By extending the Thread class (java.lang.Thread)  By implementing the Runnable interface (java.lang.Runnable) Creating Threads
  • 14. class FirstThread extends Thread { public void run() { int a = 8,b = 4,sum; sum = a + b; System.out.println("Sum is "+sum); } } class ThreadDemo2{ public static void main(String args[]){ FirstThread obj1 = new FirstThread(); obj1.start(); } } extends Thread class override the run() method Use start() method to start the thread
  • 15. class FirstThread implements Runnable { public void run() { int a = 8,b = 4,sum; sum = a + b; System.out.println("Sum is "+sum); } } class ThreadDemo2{ public static void main(String args[]){ FirstThread obj1 = new FirstThread(); Thread t = new Thread(obj1); t.start(); } } Implements Runnable interface override the run() method Use start() method to start the thread
  • 16. Extending Thread vs. Implementing Runnable Interface • Choosing between these two is a matter of taste • Implementing the Runnable interface – May take more work since we still • Declare a Thread object • Call the Thread methods on this object • Extending the Thread class – Easier to implement – Your class can no longer extend any other class
  • 17. EXERCISE • Based on the situations below, identify and state the appropriate methods. i. Thread will suspend according to the time frame. ii. This method is used to schedule the thread to run again. iii. Thread will wait until some event occurs. iv. This method will resume the suspended thread. v. Thread will stop.
  • 18. Match the state with correct answer Newborn Thread is created. Runnable It is ready for execution and is waiting for the availability of CPU. Running It is the time given for the execution of the thread. Blocked The thread is suspended, sleeping or waiting to fulfil certain requirements. Dead A thread completes executing the run() method, ends its life.
  • 19. In this presentation, you learnt the following • Threads can be defined as single, sequential flow of control within a program. • The two types of threads are single and multiple threads • A process that is made of only one thread is called single threaded application. Summary
  • 20. • A program that supports more than one thread is multi-threaded application. • The lifetime of a thread includes the Newborn, Runnable, Running, Blocked and Dead states.

Editor's Notes

  • #8: multitasking: Word processor lets you print or save a file while you are typing Web browser, allow you to download a file while you are searching an information
  • #11: Initiate = begin Kill = destroy Suspend = delay the executing of the thread Resume = start again the executing
  • #13: Newborn = new state Runnable = ready state A ready thread is runnable but may not be running yet. The os has to allocate CPU time to it Complete = completes the execution of run() method