InterThread Communication in java

InterThread Communication in java
SOURCE : Customer.java






class 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
}

}

}



INPUT




OUTPUT

going for withdraw please be patience ....
balaced amount 200
amount going for deposited








Sandeep Kumar D

Hi, I have written and developed this post so that most of people will be benefited. I'm committed to provide easy and in-depth tutorials on various technologies.I hope it will help you a lot.

- Sandeep Kumar D

Follow Me @Google+




SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment