Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Inter-Thread Communication 
 

Java provides a very efficient way through which multiple-threads can communicate with each-other

 

Inter-Thread Communication

                         

Java provides a very efficient way through which multiple-threads can communicate with each-other. This way reduces the CPU’s idle time i.e. A process where, a thread is paused running in its critical region and another thread is allowed to enter (or lock) in the same critical section to be executed.  This technique is known as Interthread communication which is implemented by some methods. These methods are defined in "java.lang" package and can only be called  within synchronized code shown as:

 Method  Description
 wait( )  It indicates the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls method notify() or notifyAll().
 notify( )  It wakes up the first thread that called wait() on the same object.
 notifyAll( )  Wakes up (Unloack) all the threads that called wait( ) on the same object. The highest priority thread will run first.

All these methods must be called within a try-catch block.

Lets see an example implementing these methods :

class Shared {

int num=0;
boolean value = false;
    
synchronized int get() {
  if (value==false
  try {
    wait();
    }
  catch (InterruptedException e) {
  System.out.println("InterruptedException caught");
    }
System.out.println("consume: " + num);
value=false;
notify();
return num;
}

synchronized void put(int num) {
  if (value==true
  try {
    wait();
    }
  catch (InterruptedException e) {
  System.out.println("InterruptedException caught");
    }
    this.num=num;
    System.out.println("Produce: " + num);
    value=false;
    notify();
    }
    }

    class Producer extends Thread {
  Shared s;
  
  Producer(Shared s) {
    this.s=s;
    this.start();
  }

  public void run() {
    int i=0;
    
    s.put(++i);
    }
}

class Consumer extends Thread{
  Shared s;
  
  Consumer(Shared s) {
    this.s=s;
    this.start();
  }

  public void run() {
    s.get();
    }
}

public class InterThread{
  public static void main(String[] args) 
  {
    Shared s=new Shared();
    new Producer(s);
    new Consumer(s);
  }
}

Output of the Program:

C:\nisha>javac InterThread.java

C:\nisha>java InterThread
Produce: 1
consume: 1

In this program, two threads "Producer" and "Consumer" share the synchronized methods of the class "Shared". At time of program execution, the "put( )" method is invoked through the "Producer" class which increments the variable "num" by 1. After producing 1 by the producer, the method "get( )" is invoked by through the "Consumer" class which retrieves the produced number and returns it to the output. Thus the Consumer can't retrieve the number without producing of it. 

Download this Program

Another program demonstrates the uses of wait() & notify() methods:

public class DemoWait extends Thread{
  int val=20;
  public static void main(String args[])  {
    DemoWait d=new DemoWait();
    d.start();
    new Demo1(d);
  }
  public void run(){
    try    {
    synchronized(this){
    wait();
    System.out.println("value is  :"+val);    
    }
    }catch(Exception e){}
 
}

  public void valchange(int val){    
    this.val=val;    
    try    {
    synchronized(this)    {
    notifyAll();   
    }
    }catch(Exception e){}

  }
}
class Demo1 extends Thread{
  DemoWait d;
  Demo1(DemoWait d)  {
  this.d=d;
  start();
  }
  public void run(){
   try{
    System.out.println("Demo1 value is"+d.val);
    d.valchange(40);
  }catch(Exception e){}
  }
}

Output of the program is:

C:\j2se6\thread>javac DemoWait.java

C:\j2se6\thread>java DemoWait
Demo1 value is20
value is :40

C:\j2se6\thread>

Download this example

                         

» View all related tutorials
Related Tags: java c class interface object io read thread int this creation extend run lan ext e il section can im

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

Interthread Class code

it needes slight modification to get desired output:

class Shared {

int num=0;
boolean value = false;

synchronized int get() {
if (value==false)
try {
wait();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
System.out.println("consume: " + num);
value=false;
notify();
return num;
}

synchronized void put(int num) {
if (value==true)
try {
wait();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
this.num=num;
System.out.println("Produce: " + num);
value=true;
notify();
}
}

Posted by Avneesh Saini on Friday, 08.8.08 @ 12:53pm | #71769

This site is really Nice. Its helping to Programmer in very easy way.

Thanks and Regards:Arun

Posted by arun on Thursday, 01.17.08 @ 17:04pm | #45443

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.