-
May 13th, 2016, 09:55 PM
#1
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
-
May 14th, 2016, 12:16 AM
#2
Re: Multithreading
Determine which class fields are accessed within the threads and synchronize them.
-
May 14th, 2016, 09:45 AM
#3
Re: Multithreading
Can you post examples of
the current, incorrect output
and one showing the correct output?
Norm
-
May 14th, 2016, 10:03 AM
#4
Re: Multithreading
Originally Posted by Norm
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.
-
May 14th, 2016, 10:42 AM
#5
Re: Multithreading
Sorry, My post was directed at KChad to provide some documentation on his problem.
Norm
-
May 16th, 2016, 09:34 PM
#6
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
-
May 17th, 2016, 06:08 AM
#7
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
-
May 17th, 2016, 04:21 PM
#8
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.
-
May 17th, 2016, 04:24 PM
#9
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
-
May 17th, 2016, 04:30 PM
#10
Re: Multithreading
Can this be solved using Synchronization; keeping the threads in place.
-
May 17th, 2016, 04:36 PM
#11
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
-
May 17th, 2016, 04:39 PM
#12
Re: Multithreading
How about using semaphores?
-
May 17th, 2016, 04:41 PM
#13
Re: Multithreading
ok lets forget about running them in order. lets them run simultaneously without a race condition
-
May 17th, 2016, 04:42 PM
#14
-
May 17th, 2016, 04:43 PM
#15
Re: Multithreading
but how? this is where I am stuck
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|