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 4 of 4

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
    24,531
    Thanks
    55
    Thanked 2,659 Times in 2,609 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
    24,531
    Thanks
    55
    Thanked 2,659 Times in 2,609 Posts

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

    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jun 2021
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Web development is typically divided into two categories: web design and web services. Design encompasses the overall look and feel of the web pages; while services covers the functionality of the software by https://mlsdev.com. Typically, design happens in stages with development following close behind. The software development team is responsible for laying out the entire visual design of the web site as well as doing the functional programming.
    Last edited by katespy; September 20th, 2021 at 05:49 AM.

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