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

Thread: Simple Bank Account Program

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Bank Account Program

    Hi,

    I have a simple program for testing threads below for the standard bank account problem. Can someone explain why I'm getting the following output? I thought that having a synchronized keyword would ensure that the thread would complete the entire method. Thanks.
    Jack, Previous balance: 33.0, New balance: 34.0
    Jack, Previous balance: 34.0, Jill, Previous balance: 0.0, New balance: 36.0
    Jill, Previous balance: 36.0, New balance: 37.0

    public class BankAccountThread extends Thread {
     
    	/* Shared variable that is accessed by multiple threads */
    	private static double accountBalance;
     
    	public BankAccountThread(String name) {
    		super(name); 
    	}	
     
    	public synchronized void deposit(double amount) {
    		System.out.print(this.getName() + ", Previous balance: " + accountBalance + ", ");
    		accountBalance = accountBalance + amount; 
    		System.out.print("New balance: " + accountBalance + "\n");
    	}
     
    	public void run() {
    		for (int i=0; i < 100; i++) {
    			deposit(1);
    		}
    	}	
     
    	public static void main(String[] args) {
    		new BankAccountThread("Jack").start(); 
    		new BankAccountThread("Jill").start();
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple Bank Account Program

    I recommend reading about Synchronized methods
    Synchronized Methods (The Java™ Tutorials > Essential Classes > Concurrency)
    In particular the two bullet points about half way down the page, paying special attention to the key phrase 'same object'.

Similar Threads

  1. Bank Account Program re-post
    By kfdhihi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2011, 10:29 AM
  2. Bank Account HELP!!!! PLEASE.
    By metaleddie13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 16th, 2011, 08:06 PM
  3. help with my bank account program
    By captain_turkiye in forum Object Oriented Programming
    Replies: 3
    Last Post: October 14th, 2011, 10:33 AM
  4. Bank Account Program.
    By Punky0214 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 17th, 2010, 10:42 PM
  5. How to delete records from a Random-Access File?
    By keith in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: March 31st, 2009, 11:40 AM