Skip to Main Content

Java User Groups

How multiple user threads manipulate the same static variable??

4156385Dec 23 2019 — edited Dec 23 2019
Consider the banking example. We know that there is a ledgerBalance(static) and it will be manipulated by every account holder when he/she does the transaction.

class Account{

private double myBalance;

public static double ledgerBalance;

public void withdraw(double amount){

if(myBalance > 0){

ledgerBalance = ledgerBalance-amount;

myBalance = myBalance - amount;

}

}

public void deposit(){

ledgerBalance = ledgerBalance+amount;

myBalance = myBalance +amount;

}

}

My question is how the ledgerBalance(which will be shared among all the A/C holders) will be changed at the same time if two or more user threads are doing the transaction at exactly same time.

What I am thinking that it is not possible to manipulate same variable by multiple threads at exactly same time.

Comments
Post Details
Added on Dec 23 2019
0 comments
34 views