InterThread Communication in java
SOURCE : Customer.javaclass Customer { int amount = 100; synchronized void withdraw(int amount) { System.out.println("going for withdraw please be patience ...."); if (this.amount > amount) { System.out.println("doesn`t have balance please wait...."); try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } this.amount -= amount; System.out.println("balaced amount " + amount); } synchronized void deposit(int amount) { System.out.println("amount going for deposited"); this.amount += amount; notify(); }}public class InterThreadCommunication { public static void main(String[] args) { final Customer c = new Customer(); try { c.withdraw(200); c.deposit(300); } catch (Exception e) { // TODO: handle exception } }}INPUTOUTPUTgoing for withdraw please be patience ....balaced amount 200amount going for deposited

0 comments :
Post a Comment