?
Solved

threads data corruption

Posted on 2011-04-26
2
Medium Priority
?
315 Views
Last Modified: 2012-05-11
>>The following code is an attempt to illustrate how important it is to synchronize threads that might access the same data. It consists of a class called bank, that acts as a driver to create multiple threads of running the methods of a class called Business. The Business threads act to add and subtract money from the accounts. The idea of the code is to illustrate how multiple threads can "tread on each others toes" and lead to data corruption, even though there is code that attempts to avoid this corruption. In order to "fix" and ensure this corruption I have put in a call to a sleep method, which you can consider to be the equivalent to the pause that would take place when real banking code wrote to a database. The corruption that this illustrates would still occasionally happen without this call to sleep but you might have to run the code quite a few times and for quite a long time before it manifested itself.

public class Account{
    private int iBalance;
    public void add(int i){
    iBalance = iBalance + i;
    System.out.println("adding " +i +" Balance = "+ iBalance);
    }
    public  void withdraw(int i){
    if((iBalance - i) >0 ){
        try{
        Thread.sleep(60);
        }catch(InterruptedException ie){}
        iBalance = iBalance - i;
    }else{  
        System.out.println("Cannot withdraw, funds would be < 0");
    }      
    if(iBalance  < 0){
        System.out.println("Woops, funds below 0");  
        System.exit(0);
    }
   
    System.out.println("withdrawing " + i+ " Balance = " +iBalance);
    }
    public int getBalance(){
    return iBalance;
    }
}





I read above program and explanation from link
http://www.jchq.net/certkey/0703certkey.htm

It is not clear to me in tems of flow, use of synchronized, data corruption.





Any ideas, resources, links, sample code highly appreciated. thanks in advance.
0
Comment
Question by:gudii9
2 Comments
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 1000 total points
ID: 35471897
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 35479968
Say the balance of the account is 100
Then two threads at the same time try to call withdraw(100)
you will end up with the account being overdrawn

to stop this possibility we could synchronize the withdraw method so only one thread can call it at a time
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

840 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