0% found this document useful (0 votes)
4 views2 pages

Java Inter-Thread Communication Example

Uploaded by

dreamsapnapbozo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java Inter-Thread Communication Example

Uploaded by

dreamsapnapbozo
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like