Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How multiple user threads manipulate the same static variable??

Akash128janDec 30 2019 — edited Dec 30 2019

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.

Comments

Post Details

Added on Dec 30 2019
0 comments
157 views