thread communication deals the problem that if one thread leaves the monitor then how it will tell to the waiting thread that it is now free to get the monitor. java provide interprocess communication machenism through wait() ,notify() and notifyAll() methods. these method are implemented in the object class as final.
notify()
It called by the monitor owner thread to wakes up a single thread that is waiting for this monitor object. If more then one, threads are waiting for this monitor then one of them is chosen to be awakened. The choice is arbitrary.
notifyAll()
It called by the monitor owner thread to wakes up multiple threads waiting for this monitor object.
wait()
Causes current thread to wait until another thread invokes the notify () method or the notifyAll() method for this object. It throws InterruptedException
wait(long timeout)
Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
it throws InterruptedException
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object.
//Thread communication
class Common
{
synchronized void hello1(int i,String s)
{
System.out.println(i+" Hello1 : "+s);
notifyAll();
if(i<10)
try{wait();}catch(Exception e){}
}
synchronized void hello2(int i,String s)
{
System.out.println(i+" Hello2 : "+s);
notifyAll();
if(i<10)
try{wait();}catch(Exception e){}
}
}
class MyThreadA extends Thread
{
Common c=null;
MyThreadA(Common c)
{
this.c=c;
}
public void run()
{
for(int i=1;i<=10;i++)
c.hello1(i,"MyThreadA ");
}
}
class MyThreadB extends Thread
{
Common c=null;
MyThreadB(Common c)
{
this.c=c;
}
public void run()
{
for(int i=1;i<=10;i++)
c.hello2(i,"MyThreadB ");
}
}
class ThreadCommunication
{
public static void main(String ar[])
{
Common obj=new Common();
MyThreadA t1=new MyThreadA(obj);
MyThreadB t2=new MyThreadB(obj);
t1.start();
t2.start();
}
}
/*OUTPUT
1 Hello1 : MyThreadA
1 Hello2 : MyThreadB
2 Hello1 : MyThreadA
2 Hello2 : MyThreadB
3 Hello1 : MyThreadA
3 Hello2 : MyThreadB
4 Hello1 : MyThreadA
4 Hello2 : MyThreadB
5 Hello1 : MyThreadA
5 Hello2 : MyThreadB
6 Hello1 : MyThreadA
6 Hello2 : MyThreadB
7 Hello1 : MyThreadA
7 Hello2 : MyThreadB
8 Hello1 : MyThreadA
8 Hello2 : MyThreadB
9 Hello1 : MyThreadA
9 Hello2 : MyThreadB
10 Hello1 : MyThreadA
10 Hello2 : MyThreadB
*/
class Common
{
synchronized void hello1(int i,String s)
{
System.out.println(i+" Hello1 : "+s);
notifyAll();
if(i<10)
try{wait();}catch(Exception e){}
}
synchronized void hello2(int i,String s)
{
System.out.println(i+" Hello2 : "+s);
notifyAll();
if(i<10)
try{wait();}catch(Exception e){}
}
}
class MyThreadA extends Thread
{
Common c=null;
MyThreadA(Common c)
{
this.c=c;
}
public void run()
{
for(int i=1;i<=10;i++)
c.hello1(i,"MyThreadA ");
}
}
class MyThreadB extends Thread
{
Common c=null;
MyThreadB(Common c)
{
this.c=c;
}
public void run()
{
for(int i=1;i<=10;i++)
c.hello2(i,"MyThreadB ");
}
}
class ThreadCommunication
{
public static void main(String ar[])
{
Common obj=new Common();
MyThreadA t1=new MyThreadA(obj);
MyThreadB t2=new MyThreadB(obj);
t1.start();
t2.start();
}
}
/*OUTPUT
1 Hello1 : MyThreadA
1 Hello2 : MyThreadB
2 Hello1 : MyThreadA
2 Hello2 : MyThreadB
3 Hello1 : MyThreadA
3 Hello2 : MyThreadB
4 Hello1 : MyThreadA
4 Hello2 : MyThreadB
5 Hello1 : MyThreadA
5 Hello2 : MyThreadB
6 Hello1 : MyThreadA
6 Hello2 : MyThreadB
7 Hello1 : MyThreadA
7 Hello2 : MyThreadB
8 Hello1 : MyThreadA
8 Hello2 : MyThreadB
9 Hello1 : MyThreadA
9 Hello2 : MyThreadB
10 Hello1 : MyThreadA
10 Hello2 : MyThreadB
*/
Comments