class Reserve implements Runnable { int available=1; int wanted; Reserve () { wanted=1; } public void run() { System.out.println("avalable Tickets are "+available); if(available>=wanted) { String name=Thread.currentThread().getName(); System.out.println(wanted+" Ticket reserved for person "+name); try { Thread.sleep(2000); available=available-wanted; } catch(Exception e) { System.out.println(e); } } else { System.out.println("sorry ! no Tickets Available"); } } } public class Main { public static void main(String args[]) { Reserve r=new Reserve(); Thread t1=new Thread(r); Thread t2=new Thread(r); Thread t3=new Thread(r); Thread t4=new Thread(r); t1.start(); t2.start(); t3.start(); t4.start(); } }
avalable Tickets are 1
avalable Tickets are 1
avalable Tickets are 1
avalable Tickets are 1
1 Ticket reserved for person Thread-0
1 Ticket reserved for person Thread-1
1 Ticket reserved for person Thread-2
1 Ticket reserved for person Thread-3