同步
This commit is contained in:
parent
caf350e775
commit
fa4e96b1d3
21
exp12/Person.java
Normal file
21
exp12/Person.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
29
exp12/TicketingSystem/TicketingSystem.java
Normal file
29
exp12/TicketingSystem/TicketingSystem.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user