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 Thread Synchronization
PPTX
WHAT IS ABSTRACTION IN JAVA
PDF
Life cycle-of-a-thread
PPTX
PPTX
Characteristics of OOPS
PPTX
Java abstract class & abstract methods
PPTX
Abstract Class Presentation
PPTX
Cookie & Session In ASP.NET
Java Thread Synchronization
WHAT IS ABSTRACTION IN JAVA
Life cycle-of-a-thread
Characteristics of OOPS
Java abstract class & abstract methods
Abstract Class Presentation
Cookie & Session In ASP.NET

What's hot (20)

PPT
Abstract class in java
PPT
Exception Handling in JAVA
PPTX
Multithreading in java
PPTX
Static Data Members and Member Functions
PPTX
MULTI THREADING IN JAVA
PPTX
INHERITANCE IN JAVA.pptx
PPTX
Java exception handling
PPS
Java Exception handling
PPTX
JAVA AWT
PPTX
Control structures in java
PPT
Java exception
PPTX
Templates in C++
PDF
Method Overloading In Java
PPTX
Hashing
PPTX
Exception Handling in object oriented programming using C++
PPTX
OOP Introduction with java programming language
PPTX
Object oriented programming
PPTX
Control Statements in Java
PPTX
Inheritance in c++
PPT
Java oops PPT
Abstract class in java
Exception Handling in JAVA
Multithreading in java
Static Data Members and Member Functions
MULTI THREADING IN JAVA
INHERITANCE IN JAVA.pptx
Java exception handling
Java Exception handling
JAVA AWT
Control structures in java
Java exception
Templates in C++
Method Overloading In Java
Hashing
Exception Handling in object oriented programming using C++
OOP Introduction with java programming language
Object oriented programming
Control Statements in Java
Inheritance in c++
Java oops PPT
Ad

Similar to Multithreading in Java (20)

PPTX
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
PDF
Java Multithreading Interview Questions PDF By ScholarHat
PPTX
Threads in Java
PPTX
Java programming PPT. .pptx
PPTX
MULTITHREADING IN JAVAMULTITHREADING IN -1.pptx
PPTX
unit-3java.pptx
PPT
Java Multithreading and Concurrency
PPTX
Multithreading in java
PPTX
Multi-threaded Programming in JAVA
DOCX
Module - 5 merged.docx notes about engineering subjects java
PPTX
Chap3 multi threaded programming
PPTX
Concept of Java Multithreading-Partially.pptx
PPTX
U4 JAVA.pptx
PPTX
Multithreading in java
PPTX
Multithreading in java
PPTX
multithreading.pptx
PPT
Thread
PPTX
multithreading to be used in java with good programs.pptx
PPTX
Multithreading in java
PPT
Programming - Java-Threads-and-Synchronization.ppt
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
Java Multithreading Interview Questions PDF By ScholarHat
Threads in Java
Java programming PPT. .pptx
MULTITHREADING IN JAVAMULTITHREADING IN -1.pptx
unit-3java.pptx
Java Multithreading and Concurrency
Multithreading in java
Multi-threaded Programming in JAVA
Module - 5 merged.docx notes about engineering subjects java
Chap3 multi threaded programming
Concept of Java Multithreading-Partially.pptx
U4 JAVA.pptx
Multithreading in java
Multithreading in java
multithreading.pptx
Thread
multithreading to be used in java with good programs.pptx
Multithreading in java
Programming - Java-Threads-and-Synchronization.ppt
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)

PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
master seminar digital applications in india
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Classroom Observation Tools for Teachers
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Weekly quiz Compilation Jan -July 25.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Yogi Goddess Pres Conference Studio Updates
Final Presentation General Medicine 03-08-2024.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Final Presentation General Medicine 03-08-2024.pptx
01-Introduction-to-Information-Management.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Pharma ospi slides which help in ospi learning
master seminar digital applications in india
human mycosis Human fungal infections are called human mycosis..pptx
Chinmaya Tiranga quiz Grand Finale.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Classroom Observation Tools for Teachers
A systematic review of self-coping strategies used by university students to ...
Microbial diseases, their pathogenesis and prophylaxis
Anesthesia in Laparoscopic Surgery in India
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

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