import java.io.*;
public class nowaitpandc
{
static int producespeed=200;
static int consumespeed=200;
public static void main(String[] args){
if(args.length > 0)
producespeed=Integer.parseInt(args[0]);
if(args.length > 1)
consumespeed = Integer.parseInt(args[1]);
nowaitmonitor monitor = new nowaitmonitor();
new nowaitproducer(monitor,producespeed);
new nowaitconsumer(monitor,consumespeed);
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
System.exit(0);
}
}
class nowaitmonitor
{
int token=-1;
PrintWriter out = new PrintWriter(System.out,true);
boolean valueset=false;
synchronized int get(){
if(!valueset){
try{
wait();
}catch (InterruptedException e){}
}
valueset=false;
out.println("Got : "+ token);
notify();
return token;
}
synchronized void set(int value){
if(valueset){
try{
wait();
}catch(InterruptedException e){}
}
valueset=true;
token=value;
out.println("Set : "+ token);
notify();
}
}
class nowaitproducer implements Runnable
{
nowaitmonitor monitor;
int speed;
nowaitproducer (nowaitmonitor monitor,int speed){
this.monitor=monitor;
this.speed=speed;
new Thread(this,"producer").start();
}
public void run(){
int i=0;
while(true){
monitor.set(i++);
try{
Thread.sleep((int)(Math.random()*speed));
}catch (InterruptedException e){}
}
}
}
class nowaitconsumer implements Runnable
{
nowaitmonitor monitor;
int speed;
nowaitconsumer(nowaitmonitor monitor,int speed){
this.monitor=monitor;
this.speed= speed;
new Thread(this,"consumer").start();
}
public void run(){
while(true){
monitor.get();
try{
Thread.sleep((int)(Math.random()*speed));
}catch (InterruptedException e){}
}
}
}