SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
TOPIC:
Multithreading
What is Multithreading?
Multithreading in java is a process of
executing multiple threads simultaneously.
It is a conceptual programing paradigm
where a program is divided into two or
more subprograms which can be
implemented at the same time.
Life Cycle Of Thread
Life Cycle Of Thread(in details)
Newborn : When we create a new Thread
object using new operator , thread state is New
Thread .At this point thread is not alive and it’s
a state internal to java program.
Runnable: When we call start() function on
Thread object ,it’s state is changed to Runnable
. The control is given to Thread scheduler to
finish it’s execution.
Running: When thread is executing, it’s state is
changed to Running. A thread can change state to
Runnable, Dead or Blocked from running state
depends on time slicing, thread completion run()
method or waiting for some resources.
Blocked /Waiting: The programmer can make a
running thread to become inactive temporarily
for some period. In this period, the thread is
said to be in blocked state.
Dead : When the execution of run() method is
over, as the job it is meant is done, it is brought
to dead state.
Some Thread Method
Start(): Creates a new thread and make it runnable.
run(): It is used to perform action for a thread.
yield(): causes the currently executing thread object to
temporarily pause and allow other threads to execute.
suspend(): is used to suspend the thread
resume(): is used to resume the suspended thread
Sleep():A thread to sleep for specified time period using
the method sleep.
stop(): is used to stop the thread.
Wait():it is use a thread inactive foe some time.
notify():it use to resume the waited thread.
Example
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
How to create thread
There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
Thread Class: Thread class provide constructors and
methods to create and perform operations on a thread.
Thread class extends Object class and implements
Runnable interface. Example:
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
} }
Runnable Interface: The Runnable interface should be
implemented by any class whose instances are
intended to be executed by a thread. Runnable
interface have only one method named run().
Example:
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running..."); {
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
} }
Priority of a Thread
Each thread have a priority. Priorities are
represented by a number between 1 and 10. In most
cases, thread scheduler schedules the threads
according to their priority (known as preemptive
scheduling).
Type of priority:
1.public static int MIN_PRIORITY(1)
2.public static int NORM_PRIORITY(5)
3.public static int MAX_PRIORITY(10)
Priority of a Thread(Example)
class TestMultiPriority1 extends Thread{
public void run(){
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
TestMultiPriority1 m1=new TestMultiPriority1();
TestMultiPriority1 m2=new TestMultiPriority1();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
} }
Advantages of Java Multithreading
1) It doesn't block the user because threads
are independent and you can perform
multiple operations at the same time.
2) You can perform many operations together,
so it saves time.
3) Threads are independent, so it doesn't
affect other threads if an exception occurs in
a single thread.
Multithreading in java

More Related Content

PPTX
Multithreading in java
PDF
Java threads
PPSX
Java Multi-threading programming
PPTX
PPTX
Multi-threaded Programming in JAVA
PPT
Java And Multithreading
PPT
Thread model in java
PDF
Java Thread Synchronization
Multithreading in java
Java threads
Java Multi-threading programming
Multi-threaded Programming in JAVA
Java And Multithreading
Thread model in java
Java Thread Synchronization

What's hot (20)

PPTX
Method overloading
PPTX
Multithreading in java
ODP
Multithreading In Java
PPTX
Control statements in java
PDF
Threads concept in java
PPT
Java multi threading
PPTX
Structure of java program diff c- cpp and java
PPTX
07. Virtual Functions
PPT
Synchronization.37
PPTX
Inter Thread Communicationn.pptx
PDF
Java threading
PDF
Java I/o streams
PPT
Java interfaces
PPT
Exception Handling in JAVA
PDF
Arrays in Java
PPT
Abstract class in java
PPTX
java interface and packages
PPT
Basic of Multithreading in JAva
PPTX
Multithread Programing in Java
PDF
Java thread life cycle
Method overloading
Multithreading in java
Multithreading In Java
Control statements in java
Threads concept in java
Java multi threading
Structure of java program diff c- cpp and java
07. Virtual Functions
Synchronization.37
Inter Thread Communicationn.pptx
Java threading
Java I/o streams
Java interfaces
Exception Handling in JAVA
Arrays in Java
Abstract class in java
java interface and packages
Basic of Multithreading in JAva
Multithread Programing in Java
Java thread life cycle
Ad

Similar to Multithreading in java (20)

PPTX
Threads in Java
PPTX
Multithreading in java
PPTX
Multithreading in java
PPTX
Concept of Java Multithreading-Partially.pptx
PPTX
Internet Programming with Java
PPTX
Multithreading programming in java
PDF
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
PPT
multhi threading concept in oops through java
PPTX
multithreading to be used in java with good programs.pptx
PPTX
Chap3 multi threaded programming
PPTX
U4 JAVA.pptx
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPT
Session 7_MULTITHREADING in java example.ppt
PPTX
Multithreading in java
PPTX
Multi-Threading in Java power point presenetation
PPTX
multithreading.pptx
PPTX
Thread priorities in java
PPTX
MSBTE Computer Engineering JPR java. multi. threading.pptx
PPT
multithreading, creating a thread and life cycle in java.ppt
Threads in Java
Multithreading in java
Multithreading in java
Concept of Java Multithreading-Partially.pptx
Internet Programming with Java
Multithreading programming in java
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
multhi threading concept in oops through java
multithreading to be used in java with good programs.pptx
Chap3 multi threaded programming
U4 JAVA.pptx
multithreading,thread and processinjava-210302183809.pptx
Session 7_MULTITHREADING in java example.ppt
Multithreading in java
Multi-Threading in Java power point presenetation
multithreading.pptx
Thread priorities in java
MSBTE Computer Engineering JPR java. multi. threading.pptx
multithreading, creating a thread and life cycle in java.ppt
Ad

More from Arafat Hossan (20)

PPTX
Data Dictionary in System Analysis and Design
PPTX
Digital Data to Digital Signal Conversion
PPTX
Error and Exception Handling in PHP
PPTX
Bus Interface Unit(BIU) of 8086 Microprocessor
PPTX
Assembly language
PPTX
Web frameworks
PPTX
Cpu scheduling
PPTX
Semaphore
PPTX
Deadlock
PPTX
Data model
PPT
Mapping cardinalities
PPTX
Sql in dbms
PPTX
Relational algebra in DBMS
PPTX
Divisible rules
PPTX
Processes in Operating System
PPTX
All pair shortest path
PPT
Asymptotic notation
PPTX
Merge sort
PPTX
Job sequencing with deadline
PPTX
Multithreading in java
Data Dictionary in System Analysis and Design
Digital Data to Digital Signal Conversion
Error and Exception Handling in PHP
Bus Interface Unit(BIU) of 8086 Microprocessor
Assembly language
Web frameworks
Cpu scheduling
Semaphore
Deadlock
Data model
Mapping cardinalities
Sql in dbms
Relational algebra in DBMS
Divisible rules
Processes in Operating System
All pair shortest path
Asymptotic notation
Merge sort
Job sequencing with deadline
Multithreading in java

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
additive manufacturing of ss316l using mig welding
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Digital Logic Computer Design lecture notes
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Well-logging-methods_new................
PDF
composite construction of structures.pdf
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Operating System & Kernel Study Guide-1 - converted.pdf
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
additive manufacturing of ss316l using mig welding
OOP with Java - Java Introduction (Basics)
Digital Logic Computer Design lecture notes
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CYBER-CRIMES AND SECURITY A guide to understanding
Model Code of Practice - Construction Work - 21102022 .pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Well-logging-methods_new................
composite construction of structures.pdf
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx

Multithreading in java

  • 2. What is Multithreading? Multithreading in java is a process of executing multiple threads simultaneously. It is a conceptual programing paradigm where a program is divided into two or more subprograms which can be implemented at the same time.
  • 3. Life Cycle Of Thread
  • 4. Life Cycle Of Thread(in details) Newborn : When we create a new Thread object using new operator , thread state is New Thread .At this point thread is not alive and it’s a state internal to java program. Runnable: When we call start() function on Thread object ,it’s state is changed to Runnable . The control is given to Thread scheduler to finish it’s execution.
  • 5. Running: When thread is executing, it’s state is changed to Running. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion run() method or waiting for some resources.
  • 6. Blocked /Waiting: The programmer can make a running thread to become inactive temporarily for some period. In this period, the thread is said to be in blocked state. Dead : When the execution of run() method is over, as the job it is meant is done, it is brought to dead state.
  • 7. Some Thread Method Start(): Creates a new thread and make it runnable. run(): It is used to perform action for a thread. yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute. suspend(): is used to suspend the thread resume(): is used to resume the suspended thread Sleep():A thread to sleep for specified time period using the method sleep. stop(): is used to stop the thread. Wait():it is use a thread inactive foe some time. notify():it use to resume the waited thread.
  • 8. Example class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
  • 9. How to create thread There are two ways to create a thread: 1.By extending Thread class 2.By implementing Runnable interface.
  • 10. Thread Class: Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface. Example: class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
  • 11. Runnable Interface: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run(). Example: class Multi3 implements Runnable{ public void run(){ System.out.println("thread is running..."); { public static void main(String args[]){ Multi3 m1=new Multi3(); Thread t1 =new Thread(m1); t1.start(); } }
  • 12. Priority of a Thread Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread scheduler schedules the threads according to their priority (known as preemptive scheduling). Type of priority: 1.public static int MIN_PRIORITY(1) 2.public static int NORM_PRIORITY(5) 3.public static int MAX_PRIORITY(10)
  • 13. Priority of a Thread(Example) class TestMultiPriority1 extends Thread{ public void run(){ System.out.println("running thread name is:"+Thread.currentThread().getName()); System.out.println("running thread priority is:"+Thread.currentThread().getPriority()); } public static void main(String args[]){ TestMultiPriority1 m1=new TestMultiPriority1(); TestMultiPriority1 m2=new TestMultiPriority1(); m1.setPriority(Thread.MIN_PRIORITY); m2.setPriority(Thread.MAX_PRIORITY); m1.start(); m2.start(); } }
  • 14. Advantages of Java Multithreading 1) It doesn't block the user because threads are independent and you can perform multiple operations at the same time. 2) You can perform many operations together, so it saves time. 3) Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.