Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class TicketingSystem {
static final Ticket number1 = new Ticket("Number1", "open");
static final Ticket number2 = new Ticket("Number2", "open");
static Ticket[] numberList = new Ticket[] {number1, number2};
public static synchronized String getNextAvailableTicket() {
String availableNumber = null;
try {
for (Ticket number: numberList) {
if(number.getStatus() == "open") {
number.setStatus("pending"); // changing the state of the object
System.out.printf("Thread name %s - number found %s \n",Thread.currentThread().getName(), number.getName());
Thread.sleep(5000); // assume processing some business logic
number.setStatus("open"); // decide to put it back due to some error
availableNumber = number.getName();
} catch (InterruptedException e) {
return availableNumber;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment