SlideShare a Scribd company logo
Core Java
Multithreading
Mr Jayant Dalvi
Multithreading : Summary
• A thread is a lightweight sub-process, the smallest
unit of processing. Multiprocessing and
multithreading, both are used to achieve
multitasking.
• Multithreading in Java is a process of executing
multiple threads simultaneously.
• we use multithreading than multiprocessing
because threads use a shared memory area. They
don't allocate separate memory area so saves
memory
• a thread is executed inside the process. There is
context-switching between the threads. There can
be multiple processes inside the OS, and one
process can have multiple threads.
• Note: At a time one thread is executed only.
Advantages of Multithreading
• It doesn't block the user because threads are independent and you
can perform multiple operations at the same time.
• You can perform many operations together, so it saves time.
• Threads are independent, so it doesn't affect other threads if an
exception occurs in a single thread.
program: Java Thread Example by extending
Thread class
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();
}
}
Java Thread Example by implementing Runnable
interface
• If you are not extending the
Thread class,your class object
would not be treated as a thread
object.
• So you need to explicitely create
Thread class object.We are
passing the object of your class
that implements Runnable so
that your class run() method
may execute.
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();
}
}
Join() method
• The join() method waits for a thread
to die. In other words, it causes the
currently running threads to stop
executing until the thread it joins
with completes its task.
• Syntax:
• public void join()throws
InterruptedException
• public void join(long
milliseconds)throws
InterruptedException
class TestJoinMethod1 extends Thread{
public void run(){
for(int i=1;i<=5;i++){
try{
Thread.sleep(500);
}catch(Exception e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
TestJoinMethod1 t1=new TestJoinMethod1();
TestJoinMethod1 t2=new TestJoinMethod1();
TestJoinMethod1 t3=new TestJoinMethod1();
t1.start();
try{
t1.join();
}catch(Exception e){System.out.println(e);}
t2.start();
t3.start();
}
}
Program of performing two tasks by two threads
class Simple1 extends Thread{
public void run(){
System.out.println("task one");
}
}
class Simple2 extends Thread{
public void run(){
System.out.println("task two");
}
}
class TestMultitasking3{
public static void main(String
args[]){
Simple1 t1=new Simple1();
Simple2 t2=new Simple2();
t1.start();
t2.start();
}
}
Synchronization
• Synchronization in java is the capability to control the access of
multiple threads to any shared resource.
• Java Synchronization is better option where we want to allow only
one thread to access the shared resource.
Inter Thread Communication
• Inter-thread communication or Co-operation is all about allowing synchronized threads
to communicate with each other.
• Cooperation (Inter-thread communication) is a mechanism in which a thread is paused
running in its critical section and another thread is allowed to enter (or lock) in the same
critical section to be executed.It is implemented by following methods of Object class:
• wait()
• notify()
• notifyAll()
1) wait() method
Causes current thread to release the lock and wait until either another thread invokes the notify()
method or the notifyAll() method for this object, or a specified amount of time has elapsed.
notify() method
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this
object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of
the implementation. Syntax:
public final void notify()
3) notifyAll() method
Wakes up all threads that are waiting on this object's monitor. Syntax:
public final void notifyAll()
Process of InterThread Communication
• Threads enter to acquire lock.
• Lock is acquired by on thread.
• Now thread goes to waiting state if you call
wait() method on the object. Otherwise it
releases the lock and exits.
• If you call notify() or notifyAll() method,
thread moves to the notified state (runnable
state).
• Now thread is available to acquire lock.
• After completion of the task, thread releases
the lock and exits the monitor state of the
object.
Multithreading Important Questions
• Write a program that creates two threads. Each thread is instantiated from the same class. It executes a loop with
10 iterations. Each iteration displays “Welcome” message, sleeps for 200 milliseconds.
• Explain life cycle of thread
• What are different ways of creating a thread?
• What is purpose of wait() method in multithreading?
• What is multithreading? How to create Thread using Thread class?
• What is thread synchronization? How is it achieved in Java?
• List and explain methods used in inter thread communication
• What do you mean by synchronising a thread explain in detail
• What is meant by multithreading? Explain how to create thread using Runnable interface
• Write a note on Thread class and methods of thread class
• WAP to show interleaving of 2 threads and display output ABABABABABA

More Related Content

PDF
Java threads
PPTX
PPT
ADO.NET
PPTX
Packages in java
PDF
JAVA PROGRAMMING - The Collections Framework
PPTX
Control Statements in Java
PPT
Java And Multithreading
PPTX
Graph representation
Java threads
ADO.NET
Packages in java
JAVA PROGRAMMING - The Collections Framework
Control Statements in Java
Java And Multithreading
Graph representation

What's hot (20)

PPT
Java: GUI
PPTX
Hashing Technique In Data Structures
PPTX
Constructor in java
PDF
Arrays in Java
PPTX
collection framework in java
PPT
Java interfaces
PDF
Generics
PPT
Exception Handling in JAVA
PDF
Java collections
PPT
Array in Java
PDF
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
PPSX
Collections - Maps
DOCX
Joins in dbms and types
PPTX
Interface in java
PPTX
Java exception handling
PPT
SQLITE Android
PPTX
Multithreading in java
PDF
Python Variable Types, List, Tuple, Dictionary
PPT
Generic programming in java
PPT
Java packages
Java: GUI
Hashing Technique In Data Structures
Constructor in java
Arrays in Java
collection framework in java
Java interfaces
Generics
Exception Handling in JAVA
Java collections
Array in Java
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Collections - Maps
Joins in dbms and types
Interface in java
Java exception handling
SQLITE Android
Multithreading in java
Python Variable Types, List, Tuple, Dictionary
Generic programming in java
Java packages
Ad

Similar to Multithreading in Java (20)

PPTX
Concept of Java Multithreading-Partially.pptx
PPTX
Threads in Java
PPTX
1.17 Thread in java.pptx
PPT
BCA MultiThreading.ppt
PPTX
MULTI THREADING IN JAVA
PPTX
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
PPTX
U4 JAVA.pptx
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPTX
Multi-threaded Programming in JAVA
PPTX
advanced java ppt
PPTX
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
PDF
Multithreading Introduction and Lifecyle of thread
PPT
Threads in java, Multitasking and Multithreading
PPTX
Java class 6
PDF
Class notes(week 9) on multithreading
PDF
Java unit 12
PPT
Concept of Java Multithreading-Partially.pptx
Threads in Java
1.17 Thread in java.pptx
BCA MultiThreading.ppt
MULTI THREADING IN JAVA
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
U4 JAVA.pptx
OOPS object oriented programming UNIT-4.pptx
unit3multithreadingppt-copy-180122162204.pptx
unit3 Exception Handling multithreadingppt.pptx
Multi-threaded Programming in JAVA
advanced java ppt
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Multithreading Introduction and Lifecyle of thread
Threads in java, Multitasking and Multithreading
Java class 6
Class notes(week 9) on multithreading
Java unit 12
Ad

More from Jayant Dalvi (16)

PPTX
Linux System Administration
PPTX
Linux System Administration
PPTX
Structured system analysis and design
PPTX
Structured system analysis and design
PPTX
Structured system analysis and design
PPTX
Java I/O
PPTX
Information system audit 2
PPTX
Structured system analysis and design
PPTX
java- Abstract Window toolkit
PPTX
Structured system analysis and design
PPTX
Information system audit
PPTX
Information system audit
PPTX
Structured system analysis and design
PPTX
Information system audit
PPTX
Exception handling c++
PPTX
Object Oriented Programming using C++
Linux System Administration
Linux System Administration
Structured system analysis and design
Structured system analysis and design
Structured system analysis and design
Java I/O
Information system audit 2
Structured system analysis and design
java- Abstract Window toolkit
Structured system analysis and design
Information system audit
Information system audit
Structured system analysis and design
Information system audit
Exception handling c++
Object Oriented Programming using C++

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Pre independence Education in Inndia.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Lesson notes of climatology university.
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Pharma ospi slides which help in ospi learning
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Classroom Observation Tools for Teachers
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Pre independence Education in Inndia.pdf
Renaissance Architecture: A Journey from Faith to Humanism
VCE English Exam - Section C Student Revision Booklet
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Multithreading in Java

  • 2. Multithreading : Summary • A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking. • Multithreading in Java is a process of executing multiple threads simultaneously. • we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate memory area so saves memory • a thread is executed inside the process. There is context-switching between the threads. There can be multiple processes inside the OS, and one process can have multiple threads. • Note: At a time one thread is executed only.
  • 3. Advantages of Multithreading • It doesn't block the user because threads are independent and you can perform multiple operations at the same time. • You can perform many operations together, so it saves time. • Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.
  • 4. program: Java Thread Example by extending Thread class 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(); } }
  • 5. Java Thread Example by implementing Runnable interface • If you are not extending the Thread class,your class object would not be treated as a thread object. • So you need to explicitely create Thread class object.We are passing the object of your class that implements Runnable so that your class run() method may execute. 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(); } }
  • 6. Join() method • The join() method waits for a thread to die. In other words, it causes the currently running threads to stop executing until the thread it joins with completes its task. • Syntax: • public void join()throws InterruptedException • public void join(long milliseconds)throws InterruptedException class TestJoinMethod1 extends Thread{ public void run(){ for(int i=1;i<=5;i++){ try{ Thread.sleep(500); }catch(Exception e){System.out.println(e);} System.out.println(i); } } public static void main(String args[]){ TestJoinMethod1 t1=new TestJoinMethod1(); TestJoinMethod1 t2=new TestJoinMethod1(); TestJoinMethod1 t3=new TestJoinMethod1(); t1.start(); try{ t1.join(); }catch(Exception e){System.out.println(e);} t2.start(); t3.start(); } }
  • 7. Program of performing two tasks by two threads class Simple1 extends Thread{ public void run(){ System.out.println("task one"); } } class Simple2 extends Thread{ public void run(){ System.out.println("task two"); } } class TestMultitasking3{ public static void main(String args[]){ Simple1 t1=new Simple1(); Simple2 t2=new Simple2(); t1.start(); t2.start(); } }
  • 8. Synchronization • Synchronization in java is the capability to control the access of multiple threads to any shared resource. • Java Synchronization is better option where we want to allow only one thread to access the shared resource.
  • 9. Inter Thread Communication • Inter-thread communication or Co-operation is all about allowing synchronized threads to communicate with each other. • Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class: • wait() • notify() • notifyAll()
  • 10. 1) wait() method Causes current thread to release the lock and wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. notify() method Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. Syntax: public final void notify() 3) notifyAll() method Wakes up all threads that are waiting on this object's monitor. Syntax: public final void notifyAll()
  • 11. Process of InterThread Communication • Threads enter to acquire lock. • Lock is acquired by on thread. • Now thread goes to waiting state if you call wait() method on the object. Otherwise it releases the lock and exits. • If you call notify() or notifyAll() method, thread moves to the notified state (runnable state). • Now thread is available to acquire lock. • After completion of the task, thread releases the lock and exits the monitor state of the object.
  • 12. Multithreading Important Questions • Write a program that creates two threads. Each thread is instantiated from the same class. It executes a loop with 10 iterations. Each iteration displays “Welcome” message, sleeps for 200 milliseconds. • Explain life cycle of thread • What are different ways of creating a thread? • What is purpose of wait() method in multithreading? • What is multithreading? How to create Thread using Thread class? • What is thread synchronization? How is it achieved in Java? • List and explain methods used in inter thread communication • What do you mean by synchronising a thread explain in detail • What is meant by multithreading? Explain how to create thread using Runnable interface • Write a note on Thread class and methods of thread class • WAP to show interleaving of 2 threads and display output ABABABABABA