java 线程 模拟卖票

 

package thread;
import javax.swing.JFrame;

public class Buy extends JFrame{
	static SalesLady lady=new SalesLady(19,0,0);
	
	public static void main(String []args){
	int money[]={10,10,5,10,5,10,5,5,10,5,10,5,5,10,5};
	Thread thread[]=new Thread[20];
	System.out.println("现在开始售票。。。");
	for(int i=0;i<money.length;i++){
		//创建money.length个顾客线程
		thread[i]=new Thread(new Customer(i+1,money[i]));
		//顾客开始买东西
		thread[i].start();
	}
	//检查是否所有顾客都已买完东西
	/*WhileLoop usage:*/
	 whileLoopFlag:
		while(true){
			for(int i=0;i<money.length;i++){
				if(thread[i].isAlive()){
					continue   whileLoopFlag;
				}
			}
			break;
		}
		
	System.out.println("售票结束了。");
	}
}
/**
 * 售货员
 * @author Administrator
 *
 */
class SalesLady{
	
	int items;//商品数量
	int fiveNum;//5角数量
	int tenNum;//1元数量
	private String response="";
	/**
	 * 创建售货员
	 * @param items 商品数量
	 * @param fiveNum 5角数量
	 * @param tenNum 1元数量
	 */
	public SalesLady(int items,int fiveNum,int tenNum){
		this.items=items;
		this.fiveNum=fiveNum;
		this.tenNum=tenNum;
	}
	public synchronized String ruleOfSale(int num,int money){
		//synchronized(this);
		if(items==0){
			return "已经卖完了";
		}
		if(money==5){
			fiveNum++;
			items--;
			response="给你一个纪念品,你的钱正好。";
		}
		else if(money==10){
			while(fiveNum<1){
				try {
					System.err.println(""+num+"号顾客用10元购票,发生等待!");
					this.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			items--;
			fiveNum-=1;
			tenNum++;
			response="给你一个纪念品,你给了10元,找你5元。";
		}
		//唤起等待的现场
		this.notify();
		return response;
	}
}
/**
 * 顾客
 * @author Administrator
 *
 */
class Customer implements Runnable{
	int num;//顾客编号
	int money;//顾客使用的钱
	public Customer(int num,int money){
		this.num=num;
		this.money=money;
	}
	
	public void run() {
		try {
			Thread.sleep(300);
			System.out.println("我是"+num+"号顾客,我用"+money+"元购买纪念品,售货员说"+Buy.lady.ruleOfSale(num, money));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		//System.out.println("我是"+num+"号顾客,我用"+money+"元购买纪念品,售货员说"+Buy.lady.ruleOfSale(num, money));	
	}
}

 

 

 

Java模拟卖票可以通过多线程来实现,这是因为多线程允许在同一个程序内模拟多个操作同时进行的场景。以下是一个简单的Java线程卖票程序的示例: 首先,创建一个`TicketWindow`类,该类模拟了一个售票窗口。在这个类中,定义了票的数量以及`sellTicket()`方法,用于售票操作。为了避免线程安全问题,使用`synchronized`关键字同步`sellTicket()`方法,确保同一时刻只有一个线程可以执行这个方法。 ```java public class TicketWindow { private int ticketNum = 100; // 假设有100张票 public synchronized boolean sellTicket() { if (ticketNum > 0) { System.out.println(Thread.currentThread().getName() + " 卖出一张票,剩余票数:" + --ticketNum); return true; } else { System.out.println("票已售罄!"); return false; } } } ``` 然后,创建一个`SellTicketThread`类继承自`Thread`,表示一个卖票线程。在这个类中,使用`TicketWindow`对象来调用`sellTicket()`方法。 ```java public class SellTicketThread extends Thread { private TicketWindow window; public SellTicketThread(TicketWindow window, String name) { super(name); this.window = window; } @Override public void run() { while (true) { window.sellTicket(); if (window.getTicketNum() == 0) { break; } // 这里可以增加一个休眠时间模拟真实场景下售票的操作时间 try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 最后,在主函数中创建`TicketWindow`对象和多个`SellTicketThread`线程,启动这些线程模拟多个售票窗口同时售票。 ```java public class TicketSaleDemo { public static void main(String[] args) { TicketWindow window = new TicketWindow(); for (int i = 1; i <= 10; i++) { new SellTicketThread(window, "售票窗口" + i).start(); } } } ``` 通过上述示例,我们可以看到如何用多线程Java模拟卖票的过程。需要注意的是,实际应用中还应该考虑更多的细节和异常处理,以确保程序的健壮性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值