PROGRAM NO:- 12
AIM:- Write a program in java to show the use of inter-thread communication
with synchronization, wait(), and notify() methods.
SOURCE CODE:-
public class Test{
public static void main(String args[]){
final Customer c=new Customer();
new Thread()
{
public void run(){[Link](15000);}
}.start();
new Thread(){
public void run(){[Link](10000);}
}.start(); } }
class Customer{
int amount=10000;
synchronized void withdraw(int amount){
[Link]("going to withdraw...");
if([Link]<amount){ //10000<15000
[Link]("Less balance; waiting for deposit..." );
try{wait();}catch(Exception e){} }
[Link]-=amount; //20000-15000
[Link]("...amount= " + [Link]);//5000
[Link]("withdraw completed...");}
synchronized void deposit(int amount){
[Link]("going to deposit...");
[Link]+=amount; //10000+10000
[Link]("deposit completed...amount= " + [Link]); //20000
notify(); } }
NAMIT GUPTA,
IT-A,
163
OUTPUT:-
NAMIT GUPTA,
IT-A,
163