SlideShare a Scribd company logo
7
Most read
8
Most read
14
Most read
Inter-thread communication
&
Runnable Interface
Inter-thread communication
• 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()
Methods:-
• wait()
• notify()
• notifyAll()
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()
notifyAll() method
• Wakes up all threads that are waiting on this
object's monitor.
Syntax:
public final void notifyAll()
Process of inter-thread
communication
Explantion of above Diagram:-
• 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.
Runnable interface
• The Runnable interface should be implemented by
any class whose instances are intended to be
executed by a thread.
• The class must define a method of no arguments
called run .
• This interface is designed to provide a common
protocol for objects that wish to execute code while
they are active.
Create a Thread by Implementing a
Runnable Interface
• If your class is intended to be executed as a
thread then you can achieve this by
implementing a Runnable interface.
• You will need to follow three basic steps.
Step 1
• As a first step, you need to implement a run()
method provided by a Runnable interface.
This method provides an entry point for the
thread and you will put your complete
business logic inside this method.
• Following is a simple syntax of the run()
method
• public void run( )
Step 2
• As a second step, you will instantiate
a Thread object using the following
constructor
• Thread(Runnable threadObj, String threadName);
• Where, threadObj is an instance of a class that
implements the Runnable interface
and threadName is the name given to the
new thread.
Step 3
• Once a Thread object is created, you can start it by
calling start() method, which executes a call to run( )
method. Following is a simple syntax of start() method.
• void start();
• Example:
 Write a JAVA program which will create two child
threads by implementing Runnable interface; one
thread will print even nos from 1 to 50 and other
display vowels from accepted string.
import java.io.*;
import java.util.*;
class Even implements Runnable
{
public void run()
{
for(int i=1;i<=50;i++)
{
if(i%2==0)
System.out.println("Even no " +i);
}
}
}
class Vowel implements Runnable
{
public void run()
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string");
String data=br.readLine();
int len=data.length();
for(int i=0;i<len;i++)
{
if(data.charAt(i)=='a'|| data.charAt(i)=='e'|| data.charAt(i)=='i'|| data.charAt(i)=='o'|| data.charAt(i)=='u' ||
data.charAt(i)=='A'|| data.charAt(i)=='E'|| data.charAt(i)=='I'|| data.charAt(i)=='O'|| data.charAt(i)=='U')
System.out.println("Vowel in entered string at position "+(i+1)+" is "+data.charAt(i)); } }
catch(Exception e){} }
}
class EvenVowel
{
public static void main(String args[])
{
try
{
Even no=new Even();
Thread t1=new Thread(no);
t1.start();
t1.sleep(1000);
Vowel ch=new Vowel();
Thread t2=new Thread(ch);
t2.start();
}
catch(Exception e){}
}
}

More Related Content

ODP
Multithreading In Java
PPTX
6. static keyword
PPTX
Java RMI
PPT
Java And Multithreading
PPTX
Multithreading in java
PPT
Data Structure and Algorithms Arrays
PPTX
Chapter 07 inheritance
Multithreading In Java
6. static keyword
Java RMI
Java And Multithreading
Multithreading in java
Data Structure and Algorithms Arrays
Chapter 07 inheritance

What's hot (20)

PPT
Thread model in java
PPT
Chap2 2 1
PPT
Inheritance in java
PPTX
java interface and packages
PPTX
Introduction to triggers
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
Multi-threaded Programming in JAVA
PPT
Abstract class in java
PPTX
Multithreading in java
PPTX
I/O Streams
PPTX
PPTX
Java program structure
PPTX
Remote Method Innovation (RMI) In JAVA
PDF
Java arrays
PPT
Applet life cycle
PPTX
Method overloading
PPTX
Java applet
PPTX
Packages in java
PPTX
Packages in java
PPTX
6.applet programming in java
Thread model in java
Chap2 2 1
Inheritance in java
java interface and packages
Introduction to triggers
Abstract Class & Abstract Method in Core Java
Multi-threaded Programming in JAVA
Abstract class in java
Multithreading in java
I/O Streams
Java program structure
Remote Method Innovation (RMI) In JAVA
Java arrays
Applet life cycle
Method overloading
Java applet
Packages in java
Packages in java
6.applet programming in java
Ad

Similar to Inter thread communication &amp; runnable interface (20)

PPTX
Multithreaded programming
DOCX
Threadnotes
PPT
this power point presentation is about concurrency and multithreading
PPTX
Multithreading in java
PPTX
Multithreading in java
PPTX
Chap3 multi threaded programming
PDF
Multithreading Introduction and Lifecyle of thread
PPT
Runnable interface.34
PPT
Md09 multithreading
PPTX
Multithreading
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
PPTX
multithreading.pptx
PPTX
econtent thread in java.pptx
PPT
BCA MultiThreading.ppt
PPTX
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
PPTX
multithreading to be used in java with good programs.pptx
PPT
cs4240-multithreading.ppt presentation on multi threading
PDF
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
Multithreading.pptx
Multithreaded programming
Threadnotes
this power point presentation is about concurrency and multithreading
Multithreading in java
Multithreading in java
Chap3 multi threaded programming
Multithreading Introduction and Lifecyle of thread
Runnable interface.34
Md09 multithreading
Multithreading
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
multithreading.pptx
econtent thread in java.pptx
BCA MultiThreading.ppt
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
multithreading to be used in java with good programs.pptx
cs4240-multithreading.ppt presentation on multi threading
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
OOPS object oriented programming UNIT-4.pptx
Multithreading.pptx
Ad

Recently uploaded (20)

PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Pre independence Education in Inndia.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Lesson notes of climatology university.
PDF
Basic Mud Logging Guide for educational purpose
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
VCE English Exam - Section C Student Revision Booklet
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharma ospi slides which help in ospi learning
Pre independence Education in Inndia.pdf
Microbial disease of the cardiovascular and lymphatic systems
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Lesson notes of climatology university.
Basic Mud Logging Guide for educational purpose
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Cell Structure & Organelles in detailed.
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...

Inter thread communication &amp; runnable interface

  • 2. Inter-thread communication • 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()
  • 4. 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.
  • 5. 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()
  • 6. notifyAll() method • Wakes up all threads that are waiting on this object's monitor. Syntax: public final void notifyAll()
  • 8. Explantion of above Diagram:- • 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.
  • 9. Runnable interface • The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. • The class must define a method of no arguments called run . • This interface is designed to provide a common protocol for objects that wish to execute code while they are active.
  • 10. Create a Thread by Implementing a Runnable Interface • If your class is intended to be executed as a thread then you can achieve this by implementing a Runnable interface. • You will need to follow three basic steps.
  • 11. Step 1 • As a first step, you need to implement a run() method provided by a Runnable interface. This method provides an entry point for the thread and you will put your complete business logic inside this method. • Following is a simple syntax of the run() method • public void run( )
  • 12. Step 2 • As a second step, you will instantiate a Thread object using the following constructor • Thread(Runnable threadObj, String threadName); • Where, threadObj is an instance of a class that implements the Runnable interface and threadName is the name given to the new thread.
  • 13. Step 3 • Once a Thread object is created, you can start it by calling start() method, which executes a call to run( ) method. Following is a simple syntax of start() method. • void start(); • Example:  Write a JAVA program which will create two child threads by implementing Runnable interface; one thread will print even nos from 1 to 50 and other display vowels from accepted string.
  • 14. import java.io.*; import java.util.*; class Even implements Runnable { public void run() { for(int i=1;i<=50;i++) { if(i%2==0) System.out.println("Even no " +i); } } } class Vowel implements Runnable { public void run() { try { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the string"); String data=br.readLine(); int len=data.length(); for(int i=0;i<len;i++) { if(data.charAt(i)=='a'|| data.charAt(i)=='e'|| data.charAt(i)=='i'|| data.charAt(i)=='o'|| data.charAt(i)=='u' || data.charAt(i)=='A'|| data.charAt(i)=='E'|| data.charAt(i)=='I'|| data.charAt(i)=='O'|| data.charAt(i)=='U') System.out.println("Vowel in entered string at position "+(i+1)+" is "+data.charAt(i)); } } catch(Exception e){} } }
  • 15. class EvenVowel { public static void main(String args[]) { try { Even no=new Even(); Thread t1=new Thread(no); t1.start(); t1.sleep(1000); Vowel ch=new Vowel(); Thread t2=new Thread(ch); t2.start(); } catch(Exception e){} } }