-共有1000张电影票,可以在两个窗口领取,假设每次领取的时间为3000毫秒 java代码
时间: 2024-02-17 08:01:57 浏览: 97
java 电影售票系统
5星 · 资源好评率100%
下面是一个简单的Java代码示例,用于模拟两个窗口同时领取1000张电影票的情况,每次领取的时间为3000毫秒:
```
public class TicketCounter {
public static void main(String[] args) {
int numTickets = 1000;
int numWindows = 2;
int ticketsPerWindow = numTickets / numWindows;
for (int i = 1; i <= numWindows; i++) {
Window window = new Window(i, ticketsPerWindow);
window.start();
}
}
}
class Window extends Thread {
private int windowNum;
private int numTickets;
public Window(int windowNum, int numTickets) {
this.windowNum = windowNum;
this.numTickets = numTickets;
}
public void run() {
for (int i = 1; i <= numTickets; i++) {
System.out.println("Window " + windowNum + " is processing ticket " + i);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
该代码将1000张电影票分配给两个窗口,每个窗口领取的票数为500。Window类代表一个窗口,它在run()方法中循环处理票。每次处理票时,线程将休眠3000毫秒以模拟领取时间。在循环结束时,该窗口将完成所有票的处理。
阅读全文