进阶day06-线程安全-卖票案例的实现

This commit is contained in:
2026-03-07 14:55:26 +08:00
parent bb595f2be5
commit 0e35a38c3c
2 changed files with 49 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
package com.inmind.thread_safe04;
public class TicketTask implements Runnable{
//定义100张票电影票
int tickeCount = 100;
@Override
public void run() {
//有票就卖
while (true) {
if (tickeCount > 0) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "正在卖第" + tickeCount + "张电影票");
tickeCount--;
} else {
break;
}
}
}
}