Forum Stats

  • 3,727,527 Users
  • 2,245,403 Discussions
  • 7,852,848 Comments

Discussions

How multiple user threads manipulate the same static variable??

Akash128jan
Akash128jan Member Posts: 1

Hi,

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 Best Java 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. Anyone Knows the perfect answer? Please Help.

Sign In or Register to comment.