课题36有大佬能帮帮我吗

 

Java模拟卖票:

如有帮助请在我的回答上点击【采纳】!

 
//并发:两个或多个线程 同时访问一个共享资源的现象
public class Tickets implements Runnable {
	int count = 100;// 总票数
	public void run() {
		while (true) {
			// 1、给程序段加上互斥锁
			synchronized (this) {
				if (count < 1) {
					System.out.println("票已售罄");
					break;
				}
				count--;// 余票减1
				System.out.println(Thread.currentThread().getName()
						+ " 卖出了一张票,还剩" + count);
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	
	public static void main(String[] args) {
		Tickets t = new Tickets();
		Thread a = new Thread(t, "赣州站");
		Thread b = new Thread(t, "广州站");
		Thread c = new Thread(t, "上海站");
		a.start();
		b.start();
		c.start();
	}
}

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m