class Bus extends Thread { int available = 1; int passenger; Bus(int passenger) { this.passenger=passenger; } public synchronized void run() { String n = Thread.currentThread().getName(); if (available >= passenger) { System.out.println(n + " seat reserved"); available = available-passenger; } else { System.out.println("Seat not reserved"); } } } class D { public static void main(String[] args) { Bus bus = new Bus(1); Thread a = new Thread(bus); Thread s = new Thread(bus); Thread z = new Thread(bus); a.setName("raju"); z.setName("rahul"); s.setName("om"); a.start(); z.start(); s.start(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter