/*********************************************************************************
By => Ajinkya Mahajan... Xtream Kings... K.K.Wagh COE,Nashik...
Ajinkya's Blog :- http://aju-feelitsayit.blogspot.com/
*********************************************************************************/
import java.io.DataInputStream;
class p extends Thread
{
public static int pcnt=0;
public void run()
{
if(PC.n<PC.np)
{
System.out.println("Producer Producing...");
PC.n++;
System.out.println("In Stock : "+PC.n);
pcnt++;
}
else
{
System.out.println("Stock Full!!!");
}
}
}
class c extends Thread
{
public static int ccnt=0;
public void run()
{
if(PC.n>0)
{
System.out.println("Consumer Consuming...");
PC.n--;
System.out.println("In Stock : "+PC.n);
ccnt++;
}
else
{
System.out.println("Stock Empty!!!");
}
}
}
public class PC
{
public static int n=0,np,i;
public static void main(String[] args)
{
DataInputStream in=new DataInputStream(System.in);
try
{
System.out.println("Enter The Maximum Number Of Production Allowed : ");
np=Integer.parseInt(in.readLine());
System.out.println("NP : "+np);
p p1=new p();
c c1=new c();
p1.start();
c1.start();
for(i=0;;i++)
{
int n=(int) (Math.random()*50);
if(n<25)
{
p1.run();
}
else
{
c1.run();
}
if(p.pcnt==np)
break;
}
System.out.println("Total Production : "+p.pcnt);
System.out.println("Total Consumption : "+c.ccnt);
System.out.println("Total Transactions taken place(including stok empty): "+i);
}
catch(Exception e){}
}
}
No comments:
Post a Comment