I am a
java programmer but trying my first hand on threads and synchronization. I am stuck, can some one please help, The withdraw method when enters the wait state never gets the control back.
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author juahmed
*/
public class DepositWithdraw {
private int accountBalance = 0;
public int getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(int accountBalance) {
this.accountBalance = accountBalance;
}
synchronized public void withDraw(int sum){
if(accountBalance < sum){
try {
System.out.println("inside withdrawal");
wait();
} catch (InterruptedException ex) {
Logger.getLogger(DepositWithdraw.class.getName()).log(Level.SEVERE, null, ex);
}
}
else{
System.out.println("withdrawn");
setAccountBalance(accountBalance - sum);
notifyAll();
}
}
synchronized public void deposit(int sum){
setAccountBalance(accountBalance + sum);
notifyAll();
}
}
public class Withdraw implements Runnable{
String name;
public Withdraw(String name) {
this.name = name;
}
public void run(){
DepositWithdraw Dw = new DepositWithdraw();
for(int i=0;i<20;i++){
System.out.println("Initial Amount before withdrawal="+Dw.getAccountBalance());
Dw.withDraw(1);
System.out.println(" Amount after withdrawal="+Dw.getAccountBalance());
}
}
}
public class Deposit implements Runnable{
String name;
public Deposit(String name) {
this.name = name;
}
public void run(){
DepositWithdraw dw = new DepositWithdraw();
for(int i=1;i<=20;i++){
System.out.println("Initial Amount before deposit="+dw.getAccountBalance());
dw.deposit(1);
System.out.println("Amount after Deposit="+dw.getAccountBalance());
}
}
}
public class Bank {
public static void main(String[] arg){
Withdraw withdraw = new Withdraw("Manchester");
Deposit deposit = new Deposit("United");
Thread t1 = new Thread(withdraw);
Thread t2 = new Thread(deposit);
t1.start();
t2.start();
}
}
the out put is
run:
Initial Amount before withdrawal=0
inside withdrawal
Initial Amount before deposit=0
deposited
Amount after Deposit=1
Initial Amount before deposit=1
deposited
Amount after Deposit=2
Initial Amount before deposit=2
deposited
Amount after Deposit=3
Initial Amount before deposit=3
deposited
Amount after Deposit=4
Initial Amount before deposit=4
deposited
Amount after Deposit=5
Initial Amount before deposit=5
deposited
Amount after Deposit=6
Initial Amount before deposit=6
deposited
Amount after Deposit=7
Initial Amount before deposit=7
deposited
Amount after Deposit=8
Initial Amount before deposit=8
deposited
Amount after Deposit=9
Initial Amount before deposit=9
deposited
Amount after Deposit=10
Initial Amount before deposit=10
deposited
Amount after Deposit=11
Initial Amount before deposit=11
deposited
Amount after Deposit=12
Initial Amount before deposit=12
deposited
Amount after Deposit=13
Initial Amount before deposit=13
deposited
Amount after Deposit=14
Initial Amount before deposit=14
deposited
Amount after Deposit=15
Initial Amount before deposit=15
deposited
Amount after Deposit=16
Initial Amount before deposit=16
deposited
Amount after Deposit=17
Initial Amount before deposit=17
deposited
Amount after Deposit=18
Initial Amount before deposit=18
deposited
Amount after Deposit=19
Initial Amount before deposit=19
deposited
Amount after Deposit=20
amount is never withdrawn from the account balance.
Please help
Thanks in advance