⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulaterailwaystation.java

📁 通过线程进行通信...火车站售票大厅有许多窗口,有些开放,有些不开放.顾客进去火车站售票大厅后,到某个售票窗口排队等候,排到了就办理业务,然后离去.
💻 JAVA
字号:
import java.util.Date;
import java.awt.*;
import java.awt.event.*;
public class SimulateRailwayStation extends Frame implements ActionListener
{
	protected static final int NUM_AGANTS=10;
    protected static final int NUM_INITIAL_AGANTS=6;
	protected static final int BUSINESS_DELAY=6000;
	protected static final int MAX_TRAIN_NUM=10;
	protected static final int MAX_NO_CUSTOMERS=200;
	private Button  addcus=new Button("添加顾客");
    private Button  delcus=new Button("顾客离去");
	private Button  addagent=new Button("增加售票窗口");
	private Button  delagent=new Button("关闭售票窗口");
	protected static String[]  train_num={"南京->北京,46次","南京->上海,34次","南京->福州,231次","南京->杭州,65次","南京->武汉,112次","南京->成都,77次","南京->天津,21次","南京->徐州,134次","南京->乌鲁木齐,335次","南京->合肥,456次"};
	protected static int[]  tickets={50,70,50,50,50,120,60,100,50,50};
	private RailwayStation  railwaystation=new RailwayStation();
	private class WindowCloser extends WindowAdapter
	{
		public void windowClosing(WindowEvent we)
		{
		railwaystation.stop();
		System.exit(0);
		}
	}
	public SimulateRailwayStation()
	{
	super("Simulate RailwayStation");
	Panel buttons=new Panel();
	buttons.setLayout(new FlowLayout());
	buttons.add(addcus);
	buttons.add(delcus);
	buttons.add(addagent);
	buttons.add(delagent);
	addcus.addActionListener(this);
	delcus.addActionListener(this);
	addagent.addActionListener(this);
	delagent.addActionListener(this);
	addWindowListener(new WindowCloser());
	setLayout(new BorderLayout());
	add("North",railwaystation);
	add("South",buttons);
	setSize(500,200);
	validate();
	pack();
	show();
	railwaystation.start();
	}
	public void actionPerformed(ActionEvent ae)
	{
	if(ae.getSource()==addcus)
		{
	railwaystation.generateCustomer();
	}
	else if(ae.getSource()==delcus)
		{
	}
	else if(ae.getSource()==addagent)
		{
	railwaystation.addAgent();
	}
	else if(ae.getSource()==delagent)
		{railwaystation.retireAgent();}
	}
	public static void main(String args[])
	{
	SimulateRailwayStation smlt=new SimulateRailwayStation();
	}
}
class RailwayStation extends Panel implements Runnable
{
	protected Agent[] agent=new Agent[SimulateRailwayStation.NUM_AGANTS];
	protected Label[] labelAgent=new Label[SimulateRailwayStation.NUM_AGANTS];
	protected Label labelQueue=new Label("正在等待的顾客数:0");
	protected Label labelServed=new Label("已经服务的顾客数:0");
	protected int  numAgents=SimulateRailwayStation.NUM_INITIAL_AGANTS;
	public static int numCustomerServered=0;
	private Thread thread=null;
	public RailwayStation()
	{
	setup("各窗口实时状态显示:");
	}
	private void setup(String title)
	{
	Panel agentPanel=new Panel();
	agentPanel.setLayout(new GridLayout(SimulateRailwayStation.NUM_AGANTS,1));
	for(int i=0;i<SimulateRailwayStation.NUM_AGANTS;i++)
		{
	if(i<numAgents)
			{
	labelAgent[i]=new Label("窗口"+(i+1)+":空闲中...");
	agentPanel.add(labelAgent[i]);
	agent[i]=new Agent(i);
	agent[i].start();}
	else
				{
	labelAgent[i]=new Label("窗口"+(i+1)+":暂停服务!");
	agentPanel.add(labelAgent[i]);
	}
	}
	Panel otherPanel=new Panel();
	otherPanel.setLayout(new GridLayout(2,1));
	otherPanel.add(labelQueue);
	otherPanel.add(labelServed);
	setLayout(new BorderLayout());
	add("South",agentPanel);
	add("Center",otherPanel);
	add("North",new Label(title));
	}
	public void start()
		{
	if(thread==null)
			{
	thread=new Thread(this);
	thread.start();
	}
	}
	public void run(){
	while(true){
	this.updateDisplay();
	}
	}
	public void updateDisplay()
		{
	int totalSize=0;
	for(int i=0;i<numAgents;i++)
			{
	if(agent[i].getCIdOfHandling()!=0){
	totalSize+=agent[i].getCusCountOfQueue();
	String s="窗口"+(i+1)+":正在办理顾客"+agent[i].getCIdOfHandling()+"业务";
	if(agent[i].getCusCountOfQueue()>0)
		labelAgent[i].setText(s+"["+agent[i].getCusOfQueue()+"正在等待]");
	else
		labelAgent[i].setText(s);
	}
	else{
	labelAgent[i].setText("窗口"+(i+1)+":空闲中。。。");
	}
	}
	for(int i=numAgents;i<SimulateRailwayStation.NUM_AGANTS;i++)
		labelAgent[i].setText("窗口"+(i+1)+":暂停服务!");
	labelQueue.setText("正在等待的顾客数:"+totalSize);
	labelServed.setText("已经服务的顾客数:"+numCustomerServered);
	}
	public void stop()
		{
	thread=null;
	for(int i=0;i<numAgents;i++)
			{
	agent[i].halt();
	}
	}
	public void addAgent()
		{
	if(numAgents<SimulateRailwayStation.NUM_AGANTS)
			{
	agent[numAgents]=new Agent(numAgents);
	agent[numAgents].start();
	numAgents++;
	}
	}
	public void retireAgent()
		{
	if(numAgents>1)
			{
	agent[numAgents-1].halt();
	numAgents--;
	}
	}
	public void generateCustomer()
		{
	boolean allAgentQueueHasOne=true;
	for(int i=0;i<numAgents;i++)
			{
	if(agent[i].getCusCountOfQueue()==0&&agent[i].getCIdOfHandling()==0)
				{
	agent[i].joinNewCustomer(new Customer());
	allAgentQueueHasOne=false;
	break;
	}
	}
	if(allAgentQueueHasOne)
			{
	int index=0;
	for(int i=0;i<numAgents;i++)
				{
	if(agent[i].getCusCountOfQueue()<agent[index].getCusCountOfQueue())
					{
	index=i;
	}
	}
	agent[index].joinNewCustomer(new Customer());
	}
	}
	}
	class Agent extends Panel implements Runnable
	{
		private boolean running=false;
		private int ID=-1;
		private int numCustomers=0;
		private int handlingCId=0;
		private List customersofqueue=new List();
		private List customersofhandled=new List();
		private Label labelHanding=new Label();
		private Label labelThisQueue=new Label();
		private Thread thread=null;
		public Agent(int ID)
		{
		this.ID=ID;
		}
		public void start()
		{
		if(thread==null)
			{
		running=true;
		thread=new Thread(this);
		thread.start();
		}
		}
		public void halt()
		{
		running=false;
		}
		public int getCIdOfHandling()
		{
		return handlingCId;
		}
		public Customer requestCustomerFor()
		{
		if(customersofqueue.getSize()>0)
			{
		Customer c=(Customer)customersofqueue.get(0);
        customersofqueue.delete(0);
		return c;
		}
		else
			{
		return null;
		}
		}
		public int getCusCountOfHandled()
		{
		return numCustomers;
		}
		public String getCusOfHandled()
		{
		if(customersofhandled.getSize()>0)
			{
		StringBuffer sbuf=new StringBuffer();
		sbuf.append("顾客");
		for(int i=0;i<customersofhandled.getSize();i++)
				{
		sbuf.append(((Customer)customersofhandled.get(i)).getCustomerId());
		if(i!=customersofhandled.getSize()-1)
			sbuf.append(",");
		}
		return sbuf.toString();
		}
		else
			{
		return new String("");
		}
		}
       public synchronized void joinNewCustomer(Customer c)
		{
	   if(!customersofqueue.isFull())
			{
	   customersofqueue.add(c);
	   System.out.println("join to agent"+(this.ID+1));
	   }
	   }
       public synchronized String getCusOfQueue()
		{
	   if(customersofqueue.getSize()>0)
			{
	   StringBuffer sbuf=new StringBuffer();
	   sbuf.append("Customer");
	   for(int i=0;i<customersofqueue.getSize();i++)
				{
	   sbuf.append(((Customer)customersofqueue.get(i)).getCustomerId());
	   if(i!=customersofqueue.getSize()-1)
		   sbuf.append(",");
	   }
	   return sbuf.toString();
	   }
	   else
			{
	   return new String("");
	   }
	   }
	   public int getCusCountOfQueue()
		{
	   return customersofqueue.getSize();
	   }
	   public void CustomerLeft()
		{
	   if(customersofqueue.getSize()>0)
		   customersofqueue.delete(customersofqueue.getSize()-1);
	   }
	   public void releaseCustomer(Customer c)
		{
	   numCustomers++;
	   customersofhandled.add(c);
	   }
	   public void run()
		{
	   while(running)
			{
	   try{
	   thread.sleep((int)(Math.random()*SimulateRailwayStation.MAX_NO_CUSTOMERS)+1000);
	   Customer customer=requestCustomerFor();
	   if(customer!=null)
		   {
	   handlingCId=customer.getCustomerId();
	   thread.sleep((int)(Math.random()*SimulateRailwayStation.BUSINESS_DELAY)/2);
	   synchronized(this)
			   {
			   for(int i=0;i<SimulateRailwayStation.train_num.length;i++)
				   {
			   if(customer.getCustomerWilling()==i+1)
				   SimulateRailwayStation.tickets[i]--;
			   }
			   }
			   thread.sleep((int)(Math.random()*SimulateRailwayStation.BUSINESS_DELAY)/2);
			   releaseCustomer(customer);
			   RailwayStation.numCustomerServered+=1;
	   }
	   else
		   {
	   handlingCId=0;
	   }
	   }
	   catch(InterruptedException ie)
				{
	   System.out.println("Teller Exception:"+ie);
	   }
	   }
	   }
	}
	class Customer
	{
		private Date created;
		private static int cId;
		private int customerwilling=0;
		public Customer()
		{
		customerwilling=(int)(Math.random()*10+1);
		created=new Date();
		++cId;
		System.out.print("new Cusomer"+cId+",");
		}
		public int getCustomerId()
		{
		return cId;
		}
		public int getCustomerWilling()
		{
		return customerwilling;
		}
		public long getWaitTime(Date now)
		{
		return now.getTime()-created.getTime();
		}
	}
	class List
	{
		private int maxItems=100;
		private int numItems=0;
		private int ID=-1;
		private Object[] list=null;

		public List()
		{
		list=new Object[maxItems];
		}
		public List(int maxItems)
		{
		this.maxItems=maxItems;
		list=new Object[this.maxItems];
		}
		public void add(Object obj)
		{
		list[numItems++]=obj;
		}
		public void delete(int pos)
		{
		for(int i=pos+1;i<numItems;i++)
			{
		list[i-1]=list[i];
		}
		numItems--;
		}
		public Object get(int pos)
		{
		return list[pos];
		}
		public int getSize()
		{
		return numItems;
		}
		public boolean isFull()
		{
		return(numItems>=maxItems);
		}
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -