Академический Документы
Профессиональный Документы
Культура Документы
import java.util.*;
class Q {
int queue[]=new int[3],rear=-1,front=-1;
boolean full= false,empty=true;
synchronized void get()
{
while(empty)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("InterruptedException caught");
}
System.out.println("Got: " + queue[front]);
if(front==rear )
{
rear=-1;
front=-1;
empty=true;
}
else
front = front+1;
if(rear<queue.length-1)
full=false;
notify();
}
synchronized void put(int n)
{
Scanner obj=new Scanner(System.in);
System.out.println("full"+full);
while(full)
try
{ wait(); }
catch(InterruptedException e)
{ System.out.println("InterruptedException caught"); }
rear = rear+1;
if(front==-1) front++;
queue[rear]=n;
System.out.println("Put : "+n);
empty=false;
if (rear ==queue.length-1)
full=true;
notify();
}
}
class LabQueue1
{
public static void main(String args[])
{
Q q = new Q();
new Consumer(q);
new Producer(q);
}
}