| Author |
Whats Wrong in my Thread program
|
juniad Ahmed
Greenhorn
Joined: Feb 19, 2011
Posts: 24
|
|
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
Joined: Feb 19, 2011
Posts: 24
|
|
|
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
juniad Ahmed
Greenhorn
Joined: Feb 19, 2011
Posts: 24
|
|
|
Should i make the variable accoiuntbalance volatile.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
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
|
 |
 |
|
|
subject: Whats Wrong in my Thread program
|
|
|