Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java Synchronization question

Posted on 2006-04-16
8
Medium Priority
?
285 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:tiger0516
8 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467097
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.
0
 
LVL 1

Author Comment

by:tiger0516
ID: 16467121
>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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467186
Well, its not actually the best way.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16467835
Did you try the code I posted?
0
 
LVL 10

Accepted Solution

by:
Nellios earned 2000 total points
ID: 16468837
This is the classic "Racing Conditions" issue. You can google "Racing Conditions" and find lots & lots of examples.

The approach that I usally use is having a lock for each resource that is being accessed by multiple threads.

public static boolean lock=false;
public synchronized void deposit(double amount){
while(lock){
wait();
}
lock=true;
//Deposit code
lock=false;
notifyAll();
}

public synchronized void withdraw(double amount){
while(lock){
wait();
}
lock=true;
//Withdraw code
lock=false;
notifyAll();
}

0
 
LVL 9

Expert Comment

by:Manikandan Thiagarajan
ID: 16470257
synchronized is used to avoid data corruption
0
 
LVL 8

Expert Comment

by:colr__
ID: 16476025
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__
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16484892
BTW, is that the only comment you want to accept?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question