Skip to main content

::::INTERTHREAD COMMUNICATION::::



 

 

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>

Comments

Popular posts from this blog

C# Interviews Questions

Amatya  Feel free to share information Email adityagupta200@gmail.com WhatsUp 09731764134
1. What is C#? C# is an object oriented, type safe and managed language that is compiled by .Net framework to generate Microsoft Intermediate Language. 2. What are the types of comment in C# with examples? i)Single line
Eg
 // Single Line
ii. Multiple line (/* */)
Eg:
/* Amatya
Balrampur
*/
iii. XML Comments (///).Eg:/// XMLComments 3. Can multiple catch blocks be executed? No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed. 4. What is the difference between public, static and void? Public declared variables or methods are accessible anywhere in the application. Static declared variables or methods are globally accessible without creating an instance of the class. The compiler stores the address of the method as the entry point and uses this information to begin execu…

ASP .Net Basic Concept

Feel Free to share the Information Email: adityagupta200@gmail.com WhatsUp: 09731764134
Any one who want to develop a web application must have the following systems:

1. A web server.
2. An editor to develop the web pages.
3. A browser to view the web page you develop.
4. A database program like MS Access, SQL Server etc, if your web site need to save data into a database.

In the real world situation, a web server will be hosted on a secure server, located in a safe place and will be always connected to high speed internet. However, to develop a web application, you don't need to worry about security and internet connectivity. You can use your own development computer as the 'Web Server'. Web Server:-
There are several types of web servers. But if you like to develop ASP.NET web applications, you need a specific web server called 'Internet Information Server' (IIS).

IIS comes as part of Windows. But it is not installed by default, when you install Windows. Editor to d…

ANDROID TRAINING

10 AUGUST TO 31 AUGUST IN KORMANGLA,BANGALORE.

Why Learn Android App Development? The inception of this flourishing Android phone market in 2008 also gave birth to the much-evolving Google play a.k.a the Android App Store. Much like the phone’s success, the app-store has shot off to astonishing levels too. Starting with around 2000 applications in 2008, Google Play now has more than 850,000 applications in store for its users. Moreover, it boasts of more than 48 billion downloads to date, with a download rate much faster than that of iOS (103 apps downloaded every second!).
This app market has also opened wide doors to developers all around the world. With an open source concept, Android is free to develop on, prototype and deploy without paying a single cent in license fees.
Android development is also much less restrictive as compared to its counterpart, iO…