Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How multiple user threads manipulate the same static variable??

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How multiple user threads manipulate the same static variable??

    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.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,195
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How multiple user threads manipulate the same static variable??

    Yes it is possible for a statement to be executed on different threads at the same time.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,195
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How multiple user threads manipulate the same static variable??

    Also posted here: https://coderanch.com/t/724381/certi...ipulate-static
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: June 5th, 2017, 08:01 PM
  2. Threads calling static and non-static method
    By dhasija in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2014, 06:05 AM
  3. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum Java Theory & Questions
    Replies: 4
    Last Post: July 19th, 2013, 09:06 AM

Tags for this Thread