Link to home
Start Free TrialLog in
Avatar of tiger0516
tiger0516

asked on

Java Synchronization question

Hi gurus,

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()
{
this.deposit(100);
this.withdraw(200);
}

The constructor of Customer class is
public Customer(BankAcount account)
{
this.account=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.

Thanks
Avatar of Mayank S
Mayank S
Flag of India image

BTW, why does Customer extend Thread/ implement Runnable? The customer class should perhaps only hold data and the logic/ processing should be done in a separate class.

When does the thread get started and how long does it run (do you have a loop, etc in your run () method or is it just as much as you have shown?).

You can use a static object like:

private static Object synchronizer = new Object () ; // data member of the class

Then lock it while doing the transactions:

synchronized ( synchronizer )
{
  this.deposit ( 100 ) ;
  this.withdraw ( 200 ) ;

}

This will make sure that only one object of the class runs this block of code at a point of time and the other one will have to wait.
Avatar of tiger0516
tiger0516

ASKER

>BTW, why does Customer extend Thread/ implement Runnable? The customer class should perhaps only hold data and the logic/ processing should be done in a separate class.

:) It is because I think I need an example, and I thought about this, which is easy to understand.
Well, its not actually the best way.
Did you try the code I posted?
ASKER CERTIFIED SOLUTION
Avatar of Nellios
Nellios
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
synchronized is used to avoid data corruption
If you synchronize the method on an object, this means that the code within the synchronized block can only run if it gets hold of the 'object lock'. So, when the code below is run, the first thing that happens is that the thread obtains the lock for the object 'mutex'. While the thread has the lock, no other thread can obtain the lock, so you effectivvely prevent symultaneous access of the synchronised block.

synchronized(mutex){
// mutually exclusive code
...
}

The advantage of using the object 'mutuex' is that it can be declared elsewhere and can be used to limit access across threads in different classes, so you can effectively limit access to multiple methods at the same time, across multiple classes if you wanted.

The code belowalso does the same thing but works slightly differently:

synchronized{
// mutually exclusive code
...
}

In this instance, the object lock is obtained of the current instance. With this method, you can only synchronise code within the same class. This is usually acceptable for most uses of synchronisation.

colr__
BTW, is that the only comment you want to accept?