Forum Stats

  • 3,767,809 Users
  • 2,252,719 Discussions
  • 7,874,333 Comments

Discussions

How multiple user threads manipulate the same static variable??

4156385
4156385 Member Posts: 1
edited Dec 23, 2019 4:54AM in Java User Groups Discussions
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.