dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27

Thread: Multithreading

  1. #1
    Join Date
    Apr 2014
    Posts
    16

    Multithreading

    If i have an account in a local commercial bank, I can choose to have two seperate accounts under the same name. 1. A Savings account and 2. A checking account. The fee on checking account is 10% of the balance, while the interest on the savings is 10%. I deposit a $1000 on the checking account and another $1000 on my savings account. Just to balance things out, I choose a scheduled transfer of $100 from savings --> checking. This basically because 10% interest on the savings will cover the fee.

    To my suprise, my accounts fall short of expected $2000. The bank claims their banking software is perfectly alright. After entering the values I provided, the final combined value was $2000. THe banking software had three threads, i. withdraws the fee from the checking account, ii. adds interest to the savings account and iii. transfers $100 from the savings to the checking account.

    Below is the code:

    Account.Java
    Code:
    package banking;
    
    public class Account {
        final String accountHolder;
        final String accountType;
        double balance=0;
        
        public Account(String name, String type,double credit) {
            this.accountHolder = name;
            this.accountType = type;        
            this.balance=credit;
        }
        
        public void deposit(double credit) {
            balance += credit;
        }
        
        public void withdraw(double credit) {
            balance -= credit;
        }
        
        public void addinterest(double rate) {
            balance *= (100+rate)/100.0;
        }
        
    }
    Banking.java
    Code:
    package banking;
    
    public class Banking {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws InterruptedException {
            System.out.println("Application started");
            
            Account savings  = new Account("kchad","Super Saver",1000);
            Account checking = new Account("kchad","Free Checking",1000);
            
            System.out.println("\nBeginning of month");
            System.out.println(savings.accountType + ":\t"+ savings.balance);
            System.out.println(checking.accountType + ":\t"+ checking.balance);
            System.out.println("Total before \t"+ (checking.balance+savings.balance));
            
            
            Interest checkInterest = new Interest(checking, -10);        
            Interest saveInterest = new Interest(savings, 10);   
            Transfer transfer = new Transfer(savings,checking,100);  
                  
                  
            checkInterest.start();
            saveInterest.start(); 
            transfer.start();
                  
                  
            Thread.sleep(520);
            System.out.println("\nEnd of month");
            System.out.println(savings.accountType + ":\t"+ savings.balance);
            System.out.println(checking.accountType + ":\t"+ checking.balance);
            System.out.println("Total  after \t"+ (checking.balance+savings.balance));
            System.out.println("Main thread finished");
        }
    }
    Interest.java
    Code:
    package banking;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    class Interest extends java.lang.Thread {
        final Account myAccount;
        double myRate;
        
        public Interest(Account account, double rate) {
              this.myAccount=account;
              this.myRate = rate;
              setName("Interest");
        }
       
        @Override
        public void run() {
            
             System.out.println("Interest this month on "+  myAccount.accountType + ":\t" + myAccount.balance*myRate/100.0);
             myAccount.addinterest(myRate);
             
              //System.out.println(getName() + " to account " + myAccount.accountType + " successfully applied");
        }
        
        
        
    }
    Transfer.java
    Code:
    package banking;
    
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    class Transfer extends java.lang.Thread {    
        final Account myAccount1;
        Account myAccount2;
        double myAmount;
        
        public Transfer(Account account1,Account account2, double amount) {
              this.myAccount1=account1;
              this.myAccount2=account2;
              this.myAmount = amount;
              setName("Transfer");
        }
       
        @Override
        public void run() {
            
            
            myAccount1.withdraw(myAmount);
            myAccount2.deposit(myAmount);
           
            System.out.println(getName() + " from " + myAccount1.accountType + " to " + myAccount2.accountType + " successfully applied");
        }
        
        
        
    }
    How can i reproduce the problem stated. and how synchronization to solve the banking problem

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Multithreading

    Determine which class fields are accessed within the threads and synchronize them.

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Multithreading

    Can you post examples of
    the current, incorrect output
    and one showing the correct output?
    Norm

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,146

    Re: Multithreading

    Quote Originally Posted by Norm View Post
    Can you post examples of
    the current, incorrect output
    and one showing the correct output?
    No, but I can suggest that you read up on and do some tutorials on multithreading.

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Multithreading

    Sorry, My post was directed at KChad to provide some documentation on his problem.
    Norm

  6. #6
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    I ran it 5x. below is the output

    Application started

    Beginning of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total before 2000.0
    Transfer from Super Saver to Free Checking successfully applied
    Interest this month on Super Saver: 90.0
    Interest this month on Free Checking: -110.0

    End of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total after 2000.0
    Main thread finished

    ======================

    Application started

    Beginning of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total before 2000.0
    Transfer from Super Saver to Free Checking successfully applied
    Interest this month on Super Saver: 90.0
    Interest this month on Free Checking: -110.0

    End of month
    Super Saver: 990.0000000000001
    Free Checking: 990.0
    Total after 1980.0
    Main thread finished

    ========================

    Application started

    Beginning of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total before 2000.0
    Interest this month on Free Checking: -100.0
    Interest this month on Super Saver: 100.0
    Transfer from Super Saver to Free Checking successfully applied

    End of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total after 2000.0
    Main thread finished

    ==========================

    Application started

    Beginning of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total before 2000.0
    Interest this month on Super Saver: 100.0
    Transfer from Super Saver to Free Checking successfully applied
    Interest this month on Free Checking: -110.0

    End of month
    Super Saver: 990.0000000000001
    Free Checking: 990.0
    Total after 1980.0
    Main thread finished

    ===================

    Application started

    Beginning of month
    Super Saver: 1000.0
    Free Checking: 1000.0
    Total before 2000.0
    Interest this month on Free Checking: -100.0
    Transfer from Super Saver to Free Checking successfully applied
    Interest this month on Super Saver: 90.0

    End of month
    Super Saver: 990.0000000000001
    Free Checking: 1000.0
    Total after 1990.0
    Main thread finished

  7. #7
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Multithreading

    Thanks.
    The program creates and starts three threads. Are there any requirements for which thread runs first and which thread should run last? Can the threads be run in any order?
    As coded the threads can execute in any order, eg the transfer could be run first.
    Norm

  8. #8
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    Presumably the checking (fee) thread should run before the savings (interest) thread. And in order for things to reach a perfect world scenario, we need to avoid the race conditions. So this is exactly where i am stuck. I understand any thread can run last but lets put checking to be the very first.

  9. #9
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Multithreading

    Why have threads if there is an order of execution requirement?
    The Thread class's join() method can be used wait for a thread to end.
    Norm

  10. #10
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    Can this be solved using Synchronization; keeping the threads in place.

  11. #11
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Multithreading

    Why use threads? If one task MUST be run after the others, it shouldn't be on a thread that is executing at the same time as the other tasks.

    I don't see synchronization being any use to serialize the execution of tasks.
    Norm

  12. #12
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    How about using semaphores?

  13. #13
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    ok lets forget about running them in order. lets them run simultaneously without a race condition

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Multithreading

    Yes, that might work.
    Norm

  15. #15
    Join Date
    Apr 2014
    Posts
    16

    Re: Multithreading

    but how? this is where I am stuck

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)