threads data corruption

Posted on 2011-04-26
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 ){
        }catch(InterruptedException ie){}
        iBalance = iBalance - i;
        System.out.println("Cannot withdraw, funds would be < 0");
    if(iBalance  < 0){
        System.out.println("Woops, funds below 0");  
    System.out.println("withdrawing " + i+ " Balance = " +iBalance);
    public int getBalance(){
    return iBalance;

I read above program and explanation from link

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.
Question by:gudii9
    LVL 17

    Accepted Solution

    LVL 92

    Assisted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now