Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Whats Wrong in my Thread program

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
juniad Ahmed
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

juniad Ahmed wrote:
amount is never withdrawn from the account balance.



That's because there is nothing to withdraw. Your other thread is depositing to a completely different account.

Henry
 
juniad Ahmed
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should i make the variable accoiuntbalance volatile.
 
Henry Wong
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

juniad Ahmed wrote:Should i make the variable accoiuntbalance volatile.




This is *not* a thread synchronization issue. Your two threads are working with two different accounts (one created at line 53, the other at line 74). They are working independently of each other. There is nothing to synchronize.

Before you "fix" synchronization, you need to get them working on the same account first.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic