Skip to content

Instantly share code, notes, and snippets.

@raghuvarmabh
Last active August 9, 2019 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raghuvarmabh/eccd9e9f1221fe4501c8f2c8d8da55dd to your computer and use it in GitHub Desktop.
Save raghuvarmabh/eccd9e9f1221fe4501c8f2c8d8da55dd to your computer and use it in GitHub Desktop.
public static String getNextAvailableTicket() {
String availableNumber = null;
try {
synchronized(TicketingSystem.class){
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();
break;
}
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment