Created
March 11, 2014 12:42
-
-
Save eddmann/9484847 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
class PurchaseOrder { | |
private final boolean reserved; | |
private final int clientId; | |
public PurchaseOrder(boolean reserved, int clientId) | |
{ | |
this.reserved = reserved; | |
this.clientId = clientId; | |
} | |
public boolean isReserved() | |
{ | |
return reserved; | |
} | |
public int getClientId() | |
{ | |
return clientId; | |
} | |
} | |
class ConcertTicketClient extends Thread { | |
private int clientId; | |
private ConcertTicketService service; | |
private int purchased; | |
public ConcertTicketClient(int clientId, ConcertTicketService service) | |
{ | |
this.clientId = clientId; | |
this.service = service; | |
} | |
public int getTotalTicketsPurchased() | |
{ | |
return purchased; | |
} | |
public void run() | |
{ | |
Random rand = new Random(); | |
while (true) { | |
PurchaseOrder order = service.purchaseTicket(clientId); | |
if (order.isReserved()) { | |
purchased++; | |
} else { | |
// we are done. | |
break; | |
} | |
try { Thread.sleep(1); } catch (Exception e) { } | |
} | |
} | |
} | |
class ConcertTicketService extends Thread { | |
private int tickets; | |
private int first, last; | |
private Map<Integer, Integer> history; | |
public ConcertTicketService(int tickets) | |
{ | |
this.tickets = tickets; | |
history = new HashMap<>(); | |
} | |
public synchronized PurchaseOrder purchaseTicket(int clientId) | |
{ | |
PurchaseOrder order; | |
if ( ! isSoldOut()) { | |
if (first == 0) { | |
first = (int) System.currentTimeMillis(); | |
} | |
tickets--; | |
if (isSoldOut()) { | |
last = (int) System.currentTimeMillis(); | |
} | |
history.put(clientId, history.containsKey(clientId) | |
? history.get(clientId) + 1 | |
: 1); | |
order = new PurchaseOrder(true, clientId); | |
} else { | |
order = new PurchaseOrder(false, clientId); | |
} | |
return order; | |
} | |
public synchronized boolean isSoldOut() | |
{ | |
return tickets == 0; | |
} | |
public int getSaleDuration() | |
{ | |
return last - first; | |
} | |
public int getNumberOfTicketsSoldToClient(int clientId) | |
{ | |
return history.get(clientId); | |
} | |
public void run() | |
{ | |
while ( ! isSoldOut()) { | |
try { Thread.sleep(50); } catch (Exception e) { } | |
} | |
} | |
} | |
public class ConcertTickets { | |
public static void main(String[] args) | |
{ | |
ConcertTicketService service = new ConcertTicketService(100000); | |
service.start(); | |
ConcertTicketClient[] clients = new ConcertTicketClient[10]; | |
for (int i = 0; i < clients.length; i++) { | |
clients[i] = new ConcertTicketClient(i, service); | |
clients[i].start(); | |
} | |
try { | |
service.join(); | |
for (int i = 0; i < clients.length; i++) { | |
clients[i].join(); | |
} | |
System.out.println("Duration: " + service.getSaleDuration()); | |
int received = 0; | |
for (int i = 0; i < clients.length; i++) { | |
received += clients[i].getTotalTicketsPurchased(); | |
System.out.printf( | |
"Client %d: S %d R %d\n", | |
i, service.getNumberOfTicketsSoldToClient(i), clients[i].getTotalTicketsPurchased() | |
); | |
} | |
System.out.println("Total R: " + received); | |
} catch (Exception e) { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment