/*
Write a program to illustrate inter - thread communication using wait(), notify() and notify all() method
*/
import java.io.*;
class q
{
String msg;
boolean flag;
q()
{
flag=false;
}
synchronized void get()
{
if(flag==false)
{
try
{
wait();
}//try
catch(InterruptedException e)
{
}//catch
}
flag=false;
System.out.println("Received Message"+msg);
notify();
}
synchronized void put(String x)
{
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException e)
{
}//catch
}
msg = x;
flag = true;
if(msg.equalsIgnoreCase("Good Bye"))
System.exit(0);
notify();
}
}
class sender extends Thread{
q q1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
sender(q x)
{
q1 = x;
start();
}
public void run()
{
while(true)
{
System.out.println("Enter Message ");
try
{
s = br.readLine();
q1.put(s);
}
catch(IOException e)
{
}//catch
}//while
}//run
}
class receiver extends Thread
{
q q2;
receiver(q x)
{
q2 = x;
start();
}
public void run()
{
while(true)
{
q2.get();
}
}
}
class fourteen
{
public static void main(String args[])
{
q q4 = new q();
sender s = new sender(q4);
receiver r = new receiver(q4);
}
}
Write a program to illustrate inter - thread communication using wait(), notify() and notify all() method
*/
import java.io.*;
class q
{
String msg;
boolean flag;
q()
{
flag=false;
}
synchronized void get()
{
if(flag==false)
{
try
{
wait();
}//try
catch(InterruptedException e)
{
}//catch
}
flag=false;
System.out.println("Received Message"+msg);
notify();
}
synchronized void put(String x)
{
if(flag==true)
{
try
{
wait();
}
catch(InterruptedException e)
{
}//catch
}
msg = x;
flag = true;
if(msg.equalsIgnoreCase("Good Bye"))
System.exit(0);
notify();
}
}
class sender extends Thread{
q q1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
sender(q x)
{
q1 = x;
start();
}
public void run()
{
while(true)
{
System.out.println("Enter Message ");
try
{
s = br.readLine();
q1.put(s);
}
catch(IOException e)
{
}//catch
}//while
}//run
}
class receiver extends Thread
{
q q2;
receiver(q x)
{
q2 = x;
start();
}
public void run()
{
while(true)
{
q2.get();
}
}
}
class fourteen
{
public static void main(String args[])
{
q q4 = new q();
sender s = new sender(q4);
receiver r = new receiver(q4);
}
}