class Resource
{
int n;
boolean val=false;
synchronized int get()
{
while(!val)
{
try
{
wait();
}
catch(Exception e)
{}
}
System.out.println(“Consumer”+n);
try
{
Thread.sleep(500);
}
catch(Exception e1)
{}
val=false;
notify();
return n;
}
synchronized void put(int n)
{
while(val)
{
try
{
wait();
}
catch(Exception e)
{}
}
this.n=n;
val=true;
System.out.println(“producer”+n);
try
{
Thread.sleep(500);
}
catch(Exception e1)
{}
notify();
}
}
class producer implements Runnable
{
Resource q;
producer(Resource q)
{
this.q=q;
new Thread(this,”Producer”).start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class consumer implements Runnable
{
Resource q;
consumer(Resource q)
{
this.q=q;
new Thread(this,”Consumer”).start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
please help me I am not able run this program
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
4 May 2019, 16:58
looks like you got rid of the task code and replaced it with something completely different. Try resetting the task and only add comment slashes to get the required result of 19.
0