public class ElectricityBill implements Runnable { private int units; private double bill; private double unitcost=2.10; public ElectricityBill (int units){ this.units=units; } public void run(){ String tname=Thread.currentThread().getName(); System.out.println(tname +"Bill Calculation Started:"); try{ Thread.sleep(10); } catch(InterruptedException e){ e.printStackTrace(); } bill =units<=250?300:300+(units-250)*unitcost ; System.out.println("Bill Form:"+tname+" ,"+bill); } public double getBill(){ return bill; } } public class MainBill { public static void main(String[] args) { ElectricityBill house1=new ElectricityBill(200); ElectricityBill house2=new ElectricityBill(500); ElectricityBill house3=new ElectricityBill(400); Thread t1=new Thread(house1,"house1"); Thread t2=new Thread(house2,"house2"); Thread t3=new Thread(house3,"house3"); t1.start(); t2.start(); t3.start(); try{ t1.join(); t2.join(); t3.join(); }catch(InterruptedException e){ e.printStackTrace(); } double TotalRevenue=house1.getBill()+house2.getBill()+house3.getBill(); System.out.println("Total Revenue is :"+TotalRevenue); } }
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