SlideShare a Scribd company logo
1 of 25
MPhil. Scholar
Computer Science
@ SALU, Khairpur Mir’s
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.
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

What's hot

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
Jin Castor
 

What's hot (20)

Threads in python
Threads in pythonThreads in python
Threads in python
 
Java threads
Java threadsJava threads
Java threads
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
 
JDK,JRE,JVM
JDK,JRE,JVMJDK,JRE,JVM
JDK,JRE,JVM
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Java Introduction
Java IntroductionJava Introduction
Java Introduction
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
Looping statements in Java
Looping statements in JavaLooping statements in Java
Looping statements in Java
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java Collection framework
Java Collection frameworkJava Collection framework
Java Collection framework
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 

Similar to Multithreading in-java

Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
Viên Mai
 
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 Multithreading in-java (20)

Mulitthread
MulitthreadMulitthread
Mulitthread
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Thread
ThreadThread
Thread
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multi t hreading_14_10
Multi t hreading_14_10Multi t hreading_14_10
Multi t hreading_14_10
 
Multithreading.
Multithreading.Multithreading.
Multithreading.
 
Concurrency in java
Concurrency in javaConcurrency in java
Concurrency in java
 
Slot02 concurrency1
Slot02 concurrency1Slot02 concurrency1
Slot02 concurrency1
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
multithreadingppt.pptx
multithreadingppt.pptxmultithreadingppt.pptx
multithreadingppt.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
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
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 

Recently uploaded

QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
httgc7rh9c
 

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
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
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
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_...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 

Multithreading in-java

  • 1.
  • 2.
  • 3. MPhil. Scholar Computer Science @ SALU, Khairpur Mir’s
  • 4.
  • 5.
  • 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.
  • 17.
  • 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