This commit is contained in:
skyofdream 2024-05-21 17:41:05 +08:00
parent caf350e775
commit fa4e96b1d3
2 changed files with 50 additions and 0 deletions

21
exp12/Person.java Normal file
View File

@ -0,0 +1,21 @@
package exp12;
import exp12.TicketingSystem.TicketingSystem;
public class Person extends Thread{
@Override
public void run() {
while (true) {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
if ( !TicketingSystem.buy(Thread.currentThread().getName())) {
break;
}
}
}
}

View File

@ -0,0 +1,29 @@
package exp12.TicketingSystem;
import java.util.HashMap;
import java.util.Map;
public class TicketingSystem {
private int tickets = 200;
private Map<String, Integer> map = new HashMap<>();
final int MAX_PERSON = 5;
final int MAX_TICKET_PER_PERSON =10;
public static void main(String[] args) {
}
public synchronized boolean buy(String user) {
if(!map.containsValue(user) && map.size()<MAX_PERSON) {
map.put(user, 1);
this.tickets--;
}else if(map.get(user) < MAX_PERSON) {
}
return false;
}
}