多线程模拟火车站售票

生活中其实有很多多线程的例子,比如火车站售票就是一个例子。我们先来分析一下,

(1)首先要有火车票的总数量,并且每卖出一张火车票,总量就减一

(2)当火车票的数量小于1的时候,就停止售票

(3)使用多线程模拟各个窗口进行售票

(4)当火车票售完后,火车站也同样欢迎我们

下来,我们代码来实现火车站售票实例

public class MyThread implements Runnable {
    int tickets=5;
	@Override
	public void run() {
		while(true) {
			if(tickets>0) {
				try {
					Thread.sleep(100);   //让当前线程睡一会儿
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+": "+tickets--);
			}
		}
	}
 }
public class ThreadDemo {
   public static void main(String[] args) {
    //创建线程实例
	MyThread mt=new MyThread();
	
	Thread t=new Thread(mt);
	t.setName("窗口一");
	t.start();
	
	//创建线程实例
   
	Thread t2=new Thread(mt);
	t2.setName("窗口二"); //修改线程名字
	t2.start();   //启动线程
	
	Thread t3=new Thread(mt);
	t3.setName("窗口三"); //修改线程名字
	t3.start();   //启动线程
   }
}

/*
窗口三: 5
窗口一: 4
窗口二: 3
窗口三: 2
窗口一: 1
窗口二: 0
窗口三: -1
*/

观察输出结果,居然出现了-1!这不是我们想要的结果,为什么呢?因为在run()方法中,我们使用sleep()方法让线程睡了一会儿,就出现了-1这个结果,那为什么呢?

我们可以这样理解,假设此时就只剩下一张票没有卖出去了,

这时t1进来了,一看有票准备开始售票,但是他突然肚子疼就去上厕所了。

这个时候t2进来了,一看有票准备开始售票,但是他突然也肚子疼就去上厕所了。

其实这个时候相当于t1、t2已经进入if这个判断语句了。

过了一会儿t1上完厕所回来了,开始售票,这个时候所有的票已经卖完了,tickets=0。

这个时候t2也上完厕所回来了,但是他不知道票卖完了(因为他早都进入if语句里面了),然后开始售票,这个时候tickets=-1就出现了我们上面的结果了。

所以为了解决这个问题,我们引入了同步机制

synchronized: 关键字,同步(锁),可以修饰代码块和方法,一旦被某个线程访问,则直接锁住,其它线程不能访问

同步代码块

synchronized(锁){


}

注意:锁对象需要被所有线程共享

接着上面的例子,我们给其上锁

public class MyThread implements Runnable {
    int tickets=5;
    Object obj=new Object();   //锁对象
	@Override
	public void run() {
		while(true) {
			synchronized(obj){    //上锁
			if(tickets>0) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println(Thread.currentThread().getName()+": "+tickets--);
			}
		}
		}		
	} 
}
/*
窗口二: 5
窗口二: 4
窗口一: 3
窗口一: 2
窗口三: 1
*/

我们发现这个时候的输出结果是正确的。但是我们在运行的过程中发现运行速度没有之前快,这是因为加了锁以后每次都要进行开锁、关锁,所以执行速度变慢了。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的多线程模拟火车站售票大厅的程序: ```python import threading import time import random class TrainStation: def __init__(self): self.customers = [] self.tickets = 100 self.ticket_windows = 5 self.open = True def add_customer(self): self.customers.append("Customer {}".format(len(self.customers)+1)) print("Added a customer. Total customers: {}".format(len(self.customers))) def remove_customer(self): if len(self.customers) > 0: self.customers.pop(0) print("Removed a customer. Total customers: {}".format(len(self.customers))) def sell_ticket(self): if self.tickets > 0: self.tickets -= 1 print("Sold a ticket. Tickets left: {}".format(self.tickets)) def add_window(self): self.ticket_windows += 1 print("Added a ticket window. Total windows: {}".format(self.ticket_windows)) def remove_window(self): if self.ticket_windows > 0: self.ticket_windows -= 1 print("Removed a ticket window. Total windows: {}".format(self.ticket_windows)) def close_station(self): self.open = False print("Train station closed.") def customer_arrive(station): while station.open: time.sleep(random.randint(1,5)) station.add_customer() def customer_leave(station): while station.open: time.sleep(random.randint(5,10)) station.remove_customer() def sell_ticket(station): while station.open: time.sleep(1) if len(station.customers) > 0 and station.ticket_windows > 0: station.sell_ticket() def add_window(station): while station.open: time.sleep(random.randint(10,20)) station.add_window() def remove_window(station): while station.open: time.sleep(random.randint(10,20)) station.remove_window() def close_station(station): time.sleep(60) station.close_station() if __name__ == "__main__": station = TrainStation() threads = [ threading.Thread(target=customer_arrive, args=(station,)), threading.Thread(target=customer_leave, args=(station,)), threading.Thread(target=sell_ticket, args=(station,)), threading.Thread(target=add_window, args=(station,)), threading.Thread(target=remove_window, args=(station,)), threading.Thread(target=close_station, args=(station,)) ] for t in threads: t.start() for t in threads: t.join() print("Simulation finished.") ``` 这个程序模拟火车站售票大厅的场景,包括添加顾客、顾客离开、增加售票窗口、关闭售票窗口功能。在主函数中,我们创建一个 TrainStation 对象,并创建了多个线程模拟不同的行为。每个线程都会在一定的时间间隔内执行相应的操作,直到火车站关闭。最后,我们使用 join() 方法等待所有线程执行完毕,然后输出 "Simulation finished."。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值