循环数组队列(模拟北饭三的旋转火窝)

package six;
/*
 *1.循环数组(模拟北饭三的旋转火窝)
	设置缓冲区的大小是4,即数组length=4,为了让每一种情况出现的可能性大点。
	为了构建逻辑循环数组,下标取模。在数组中有一个head和tail标记,
	分别模拟旋转火窝的吃货和端菜的服务员,刚开始因为没食物,还不能开始吃。
	建一个随机数,大于等于0.5的话服务员就端上菜,小于0.5吃货吃菜。
	上菜和开吃都要有个判断,判断这个缓冲区是否满了或者空了,
	比如头追上尾时表示空,尾追上头时表示满了。这里模拟端菜+吃菜的总次数=50次。
	(端菜和吃菜两个不同的操作,建议用两个线程来做,随机数的大小来控制每个线程的挂起和进行),
	输出要求:输出每一步服务员端了什么菜,吃货吃了什么菜,缓冲区里面还有什么菜。
	若满,服务员输出“菜桌已满,无法继续端菜” ,空时,吃货输出 “没菜了,服务员上菜”
 *
 */
public class Main2
{
	public static void main(String[] args)
	{
		String[] a=new String[4]; //创建菜谱
		RotateArray<String> ra=new RotateArray<String>(a);
		String[] menu=ra.menu();
		costomer<String> c=new costomer<String>(ra);
		waiter<String> w =new waiter<String>(ra, menu);
		Thread t1=new Thread(c);
		Thread t2=new Thread(w);
		t2.start();
		t1.start();
	}
}
class RotateArray<T>
{
	public T[] tarray;	 //缓冲区
	public int head;
	public int tail;
	public RotateArray(T[] t)
	{
		this.tarray = t;
	}
	//进栈
	public synchronized boolean enqueue(T t)
	{
//		notifyAll();
		if((tail+1)%tarray.length!=head)
		{
			tarray[tail%tarray.length]=t;
			tail=(tail+1)%tarray.length;
			System.out.println("服务员上菜"+t+"  桌子有的菜:  "+this.stay());
			return true;
		}
		else
			System.out.println("菜桌已满,无法继续端菜"+"  桌子有的菜:  "+this.stay());
//		try
//		{
//			wait();
//		} catch (InterruptedException e)
//		{
//			System.out.println("costomer  error");
//		}
		return false;
		
	}
	public synchronized T outqueue()
	{
//		notifyAll();
		T t=null;
		if(tail!=head)
		{
			t=tarray[head%tarray.length];
			head=(head+1)%tarray.length;
			System.out.println("吃货者吃菜:"+t+"  桌子有的菜:  "+stay());
		}
		else
			System.out.println("没菜了,服务员上菜"+"  桌子有的菜:  "+stay());
//		try
//		{
//			wait();
//		} catch (InterruptedException e)
//		{
//			System.out.println("costomer  error");
//		}
		return t;
	}
	//创建菜单
	public String[] menu()
	{
		String[] a=new String[5];
		a[0]="1";
		a[1]="2";
		a[2]="3";
		a[3]="4";
		a[4]="5";
		return a;
	}
	//查看桌子上的菜
	public String stay()
	{
		String str="";
		for(int x=head;x!=(tail%tarray.length);)
		{
			str=str+tarray[x%tarray.length]+"  ";
			x=(x+1)%tarray.length;
		}
		return str;
	}
}
class waiter<T> implements Runnable
{
	public T[] menu;
	RotateArray<T> ra;
	public waiter(RotateArray<T> ra,T[] menu)
	{
		this.menu=menu;
		this.ra=ra;
	}
	@Override
	public void run()
	{
		for(int x=0;x<50;x++)
		{
			ra.enqueue(menu[(int)(Math.random()*menu.length)]);	
			double myrandom=Math.random();
			if(myrandom>=0.6)
			{		
				try
				{
					Thread.currentThread().sleep(100);
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
class costomer<T> implements Runnable
{
	RotateArray<T> ra;
	public costomer(RotateArray<T> ra)
	{
		this.ra=ra;
	}
	@Override
	public void run()
	{
		for(int x=0;x<50;x++)
		{
			ra.outqueue();
			double myrandom=Math.random();
			if(myrandom>=0.6)
			{		
				try
				{
					Thread.currentThread().sleep(100);
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}
发布了133 篇原创文章 · 获赞 37 · 访问量 4745

猜你喜欢

转载自blog.csdn.net/qq_43416157/article/details/104233328