SlideShare a Scribd company logo
Multithreading in-java
Multithreading in-java
MPhil. Scholar
Computer Science
@ SALU, Khairpur Mir’s
Multithreading in-java
Multithreading in-java
AGENDA
Process in OS
Thread in Java
Deference b/w Thread and Process
Multithreading in Java
Implementation of Multithreading in Java
Advantages of Multithreading
Conclusion
PROCESSOR
PROCESS IN OPERATING SYSTEM
 Process is a program in execution
 Process is not as same as program code but a lot more than it
 Process may be made up of multiple threads of execution that execute instructions
concurrently
MUSIC
PLAYER
MS-WORD
OS SWITCHING
PROCESS-1
PROCESS-1
Process Stages
A thread is a light-weight smallest part of a process that can run
concurrently with the other parts(other threads) of the same process.
Threads are independent because they all have separate path of
execution that’s the reason if an exception occurs in one thread, it
doesn’t affect the execution of other threads. All threads of a process
share the common memory. .
Thread in Java
Deference b/w Process and Thread
Threads:
1.Threads are light weight
processes. Threads are bundled
inside the process.
2.Threads have a shared memory,
data, resources, files etc.
3.Threads are created using clone()
method.
4.Context switch between the
threads are not much time
consuming as Process.
Example:
Opening multiple tabs in the
browser.
Process:
1.Process is a heavy weight process.
2.Process is a separate program that has
separate memory, data, resources etc.
3.Process are created using fork() method.
4.Context switch between the process is
time consuming.
Example:
Say, opening any browser (mozilla,
Chrome, IE). At this point new process will
start to execute.
Multiprocessing in OS
 Multiprocessing sometimes refers to
executing multiple processes (programs)
at the same time.
Multitasking :-
it is the process of executing several
task simultaneously
The process of executing multiple threads concurrently is known as
multithreading
Multithreading in Java
Let’s summarize the discussion in points:
1. The main purpose of multithreading is to provide concurrent
execution of two or more parts of a program to maximum utilize the
CPU time. A multithreaded program contains two or more parts that
can run concurrently. Each such part of a program called thread.
2. Threads are lightweight sub-processes, they share the common
memory space. In Multithreaded environment, programs that are
benefited from multithreading, utilize the maximum CPU time so that
the idle time can be kept to minimum.
A program (Process) is divided
into two or more subprograms
(process), which can be
implemented at the same time in
parallel/ concurrently
Example:- suppose we run a
program which include two
methods M1 and M2 these
methods are threads but run
concurrently
A Program
Method-1
Method-2
Multithreading :-
Multitasking
Process based Thread based
 Facebook
 Music Player
 Browser
Heavyweight
Own Memory
Address Space
MS-
Word
Home
Tab
Spelling
Check
Threads
Lightweight
Address Space
is share
Steps of Thread
Multitasking vs. Multithreading vs. Multiprocessing
vs. parallel processing
Multitasking: Ability to execute more than one task at the same
time is known as multitasking.
Multithreading: We already discussed about it. It is a process of
executing multiple threads simultaneously. Multithreading is also
known as Thread-based Multitasking.
Multiprocessing: It is same as multitasking, however in
multiprocessing more than one CPUs are involved. On the other
hand one CPU is involved in multitasking.
Parallel Processing: It refers to the utilization of multiple CPUs in a
single computer system.
Multithreading in-java
Creating a thread in Java
There are two ways to create a thread in Java:
Thread in Java
By extending
Thread class
extends
Thread
implementing
Runnable
interface
Implements
Runnable
Every time a Java program starts up, one thread begins running
which is called as the main thread of the program because it is the
one that is executed when your program begins
When you implements Runnable , you can save a space for
your class to extend any other class in future or now.
Java doesn't support multiple inheritance, which means you can
only extend one class in Java so once you extended Thread class
you lost your chance and can not extend or inherit another class in Java
Thread Creation in Java
Main Thread
Deference B/w Extending and implementing runnable class
Before we begin with the programs(code) of creating threads,
let’s have a look at these methods of Thread class.
We have used few of these methods in the example below.
•getName(): It is used for Obtaining a thread’s name
•getPriority(): Obtain a thread’s priority
•isAlive(): Determine if a thread is still running
•join(): Wait for a thread to terminate
•run(): Entry point for the thread
•sleep(): suspend a thread for a period of time
•start(): start a thread by calling its run() method
METHODS OF THREAD CLASS
Method 1: Thread creation by extending Thread class
class MultithreadingDemo implements Runnable
{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
Thread objt1 = new Thread(obj);
objt1.start();
}
}
Output:
My thread is in running state.
Method 2: Thread creation by implementing Runnable Interface
class MultithreadingDemo implements Runnable
{
public void run()
{
System.out.println("My thread is in running state.");
}
public static void main(String args[])
{
MultithreadingDemo obj=new MultithreadingDemo();
Thread tobj =new Thread(obj);
tobj.start();
}
}
A Simple Example
Output:
My thread is in running state.
Advantages of Multithreading
 Enable programmers to do multiple things at one time
 Programmers can divided a long program into threads
and execute them parallel which will eventually increases
speed of program execution
 Improved performance and concurrency
 Simultaneous access of multiple applications
Run Text Editor
Run Command Prompt
Multithreading in-java

More Related Content

PPTX
MULTI THREADING IN JAVA
PDF
Java threads
PPTX
Multithreading in Java
PPTX
Multithreading in java
PPTX
Introduction to Java -unit-1
PDF
Java IO
PPTX
Packages in java
PDF
Java thread life cycle
MULTI THREADING IN JAVA
Java threads
Multithreading in Java
Multithreading in java
Introduction to Java -unit-1
Java IO
Packages in java
Java thread life cycle

What's hot (20)

PPTX
Multithreading in java
PPT
Java multi threading
PPTX
QSpiders - Memory (JVM architecture)
PPTX
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
PPT
Java buzzwords
PPTX
Interface in java
PPT
Abstract class in java
PDF
Life cycle-of-a-thread
PDF
20 most important java programming interview questions
PPTX
Multi-threaded Programming in JAVA
PDF
Spring Boot Interview Questions | Edureka
PPT
Thread model in java
ODP
Multithreading In Java
PPTX
This keyword in java
PPTX
Presentation on java (8)
PPTX
Multithread Programing in Java
PPT
Spring ppt
PPTX
Thread model of java
PPTX
Multithreading in java
Java multi threading
QSpiders - Memory (JVM architecture)
Java Virtual Machine (JVM), Difference JDK, JRE & JVM
Java buzzwords
Interface in java
Abstract class in java
Life cycle-of-a-thread
20 most important java programming interview questions
Multi-threaded Programming in JAVA
Spring Boot Interview Questions | Edureka
Thread model in java
Multithreading In Java
This keyword in java
Presentation on java (8)
Multithread Programing in Java
Spring ppt
Thread model of java
Ad

Similar to Multithreading in-java (20)

PPT
Chap2 2 1
PPTX
Internet Programming with Java
PPTX
Concept of Java Multithreading-Partially.pptx
PPT
multhi threading concept in oops through java
PPTX
Multithreading.pptx
DOCX
Module - 5 merged.docx notes about engineering subjects java
PPT
web programming-Multithreading concept in Java.ppt
PDF
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
PPTX
Thread&multithread
PPTX
Multi-Threading in Java power point presenetation
PPT
Java Multithreading
PPT
Java multithreading
DOCX
Threadnotes
PPTX
U4 JAVA.pptx
PDF
Class notes(week 9) on multithreading
PPT
multithreading
PPT
Advanced Java Programming for Beginners.
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPT
multithreading, creating a thread and life cycle in java.ppt
Chap2 2 1
Internet Programming with Java
Concept of Java Multithreading-Partially.pptx
multhi threading concept in oops through java
Multithreading.pptx
Module - 5 merged.docx notes about engineering subjects java
web programming-Multithreading concept in Java.ppt
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
Thread&multithread
Multi-Threading in Java power point presenetation
Java Multithreading
Java multithreading
Threadnotes
U4 JAVA.pptx
Class notes(week 9) on multithreading
multithreading
Advanced Java Programming for Beginners.
multithreading,thread and processinjava-210302183809.pptx
multithreading, creating a thread and life cycle in java.ppt
Ad

Recently uploaded (20)

PDF
A systematic review of self-coping strategies used by university students to ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PPTX
Cell Types and Its function , kingdom of life
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
master seminar digital applications in india
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Lesson notes of climatology university.
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Classroom Observation Tools for Teachers
A systematic review of self-coping strategies used by university students to ...
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Cell Types and Its function , kingdom of life
Chinmaya Tiranga quiz Grand Finale.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
master seminar digital applications in india
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Supply Chain Operations Speaking Notes -ICLT Program
Lesson notes of climatology university.
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Anesthesia in Laparoscopic Surgery in India
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Classroom Observation Tools for Teachers

Multithreading in-java

  • 3. MPhil. Scholar Computer Science @ SALU, Khairpur Mir’s
  • 6. AGENDA Process in OS Thread in Java Deference b/w Thread and Process Multithreading in Java Implementation of Multithreading in Java Advantages of Multithreading Conclusion
  • 7. PROCESSOR PROCESS IN OPERATING SYSTEM  Process is a program in execution  Process is not as same as program code but a lot more than it  Process may be made up of multiple threads of execution that execute instructions concurrently MUSIC PLAYER MS-WORD OS SWITCHING PROCESS-1 PROCESS-1
  • 9. A thread is a light-weight smallest part of a process that can run concurrently with the other parts(other threads) of the same process. Threads are independent because they all have separate path of execution that’s the reason if an exception occurs in one thread, it doesn’t affect the execution of other threads. All threads of a process share the common memory. . Thread in Java
  • 10. Deference b/w Process and Thread Threads: 1.Threads are light weight processes. Threads are bundled inside the process. 2.Threads have a shared memory, data, resources, files etc. 3.Threads are created using clone() method. 4.Context switch between the threads are not much time consuming as Process. Example: Opening multiple tabs in the browser. Process: 1.Process is a heavy weight process. 2.Process is a separate program that has separate memory, data, resources etc. 3.Process are created using fork() method. 4.Context switch between the process is time consuming. Example: Say, opening any browser (mozilla, Chrome, IE). At this point new process will start to execute.
  • 11. Multiprocessing in OS  Multiprocessing sometimes refers to executing multiple processes (programs) at the same time. Multitasking :- it is the process of executing several task simultaneously
  • 12. The process of executing multiple threads concurrently is known as multithreading Multithreading in Java Let’s summarize the discussion in points: 1. The main purpose of multithreading is to provide concurrent execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread. 2. Threads are lightweight sub-processes, they share the common memory space. In Multithreaded environment, programs that are benefited from multithreading, utilize the maximum CPU time so that the idle time can be kept to minimum.
  • 13. A program (Process) is divided into two or more subprograms (process), which can be implemented at the same time in parallel/ concurrently Example:- suppose we run a program which include two methods M1 and M2 these methods are threads but run concurrently A Program Method-1 Method-2 Multithreading :-
  • 14. Multitasking Process based Thread based  Facebook  Music Player  Browser Heavyweight Own Memory Address Space MS- Word Home Tab Spelling Check Threads Lightweight Address Space is share
  • 16. Multitasking vs. Multithreading vs. Multiprocessing vs. parallel processing Multitasking: Ability to execute more than one task at the same time is known as multitasking. Multithreading: We already discussed about it. It is a process of executing multiple threads simultaneously. Multithreading is also known as Thread-based Multitasking. Multiprocessing: It is same as multitasking, however in multiprocessing more than one CPUs are involved. On the other hand one CPU is involved in multitasking. Parallel Processing: It refers to the utilization of multiple CPUs in a single computer system.
  • 18. Creating a thread in Java There are two ways to create a thread in Java: Thread in Java By extending Thread class extends Thread implementing Runnable interface Implements Runnable
  • 19. Every time a Java program starts up, one thread begins running which is called as the main thread of the program because it is the one that is executed when your program begins When you implements Runnable , you can save a space for your class to extend any other class in future or now. Java doesn't support multiple inheritance, which means you can only extend one class in Java so once you extended Thread class you lost your chance and can not extend or inherit another class in Java Thread Creation in Java Main Thread Deference B/w Extending and implementing runnable class
  • 20. Before we begin with the programs(code) of creating threads, let’s have a look at these methods of Thread class. We have used few of these methods in the example below. •getName(): It is used for Obtaining a thread’s name •getPriority(): Obtain a thread’s priority •isAlive(): Determine if a thread is still running •join(): Wait for a thread to terminate •run(): Entry point for the thread •sleep(): suspend a thread for a period of time •start(): start a thread by calling its run() method METHODS OF THREAD CLASS
  • 21. Method 1: Thread creation by extending Thread class class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread objt1 = new Thread(obj); objt1.start(); } } Output: My thread is in running state.
  • 22. Method 2: Thread creation by implementing Runnable Interface class MultithreadingDemo implements Runnable { public void run() { System.out.println("My thread is in running state."); } public static void main(String args[]) { MultithreadingDemo obj=new MultithreadingDemo(); Thread tobj =new Thread(obj); tobj.start(); } } A Simple Example Output: My thread is in running state.
  • 23. Advantages of Multithreading  Enable programmers to do multiple things at one time  Programmers can divided a long program into threads and execute them parallel which will eventually increases speed of program execution  Improved performance and concurrency  Simultaneous access of multiple applications
  • 24. Run Text Editor Run Command Prompt