Java Synchronization question
Posted on 2006-04-16
I have a question about Synchronization.
for example, I have a class,BankAccount, it has two methods, deposit(doube money), withdraw(double money).
I also have a class, Customer, which extends Thread/implements Runnable. The run() method is
public void run()
The constructor of Customer class is
public Customer(BankAcount account)
Suppose I have two customer object
Customer dad=new Customer (anAccount);
Customer son=new Customer (anAccount);
I am learing Synchronization but I am a bit confused.
How can I make sure that:
1) Only dad or son, not both, can deposit or withdraw (use sunchronized, right?)
2) Only one of dad or son, can do two transcations in order. that is, the person (the father or son) makes two transactions in pair. in other words, no such cases, the father deposits 100 dollars, and then the son withdraws 200 dollars.
I will appreciate it if you could explain thread/Synchronization a bit more.