/*
Message Class synchronizing the messages between PrintHow n PrintHello classes,
using Synchronized Methods.
*/
class Message
{
boolean f=false;
synchronized void How()
{
if(!f)
{
try
{
wait();
}
catch(Exception e)
{}
System.out.println("How");
f=false;
notify();
}
}
synchronized void Hello()
{
if(f)
{
try
{
wait();
}
catch(Exception e)
{}
System.out.println("Hello");
f=true;
notify();
}
}
}
class printhow extends Thread
{
Message m;
printhow(Message msg)
{
m = msg;
start();
}
public void run()
{
while(true)
m.How();
}
}
class printhello extends Thread
{
Message m;
printhello(Message msg)
{
m=msg;
start();
}
public void run()
{
while(true)
m.Hello();
}
}
class Interprocess
{
public static void main(String arg[])
{
Message m=new Message();
printhello po = new printhello(m);
printhow ph = new printhow(m);
}
}
Message Class synchronizing the messages between PrintHow n PrintHello classes,
using Synchronized Methods.
*/
class Message
{
boolean f=false;
synchronized void How()
{
if(!f)
{
try
{
wait();
}
catch(Exception e)
{}
System.out.println("How");
f=false;
notify();
}
}
synchronized void Hello()
{
if(f)
{
try
{
wait();
}
catch(Exception e)
{}
System.out.println("Hello");
f=true;
notify();
}
}
}
class printhow extends Thread
{
Message m;
printhow(Message msg)
{
m = msg;
start();
}
public void run()
{
while(true)
m.How();
}
}
class printhello extends Thread
{
Message m;
printhello(Message msg)
{
m=msg;
start();
}
public void run()
{
while(true)
m.Hello();
}
}
class Interprocess
{
public static void main(String arg[])
{
Message m=new Message();
printhello po = new printhello(m);
printhow ph = new printhow(m);
}
}
No comments:
Post a Comment