You are given a bank simulator program, which demonstrates concurrent transfers of money.
- The program creates n number of accounts each having n as its initial balance. n is taken from the program’s main arguments.
- The program creates a donator thread per account with a source account and a list of target accounts - a shuffled list of all accounts except the source one.
- Every donator thread transfers 1 unit of money from the source account to every recipient account sleeping 100ms after each transfer. After those transfers the thread stops.
- Bank simulator prints all balances before starting the transfers, after the transfers have finished and during the transfers once per 100ms. Printing is done in the main thread in parallel with transfers.
- Printing format: balance1 balance2 balance3 ... balancen total
Some of the things mentioned above are not implemented correctly or not implemented at all. Your task is to implement them in a thread-safe, deadlock-free manner.
Run the tests to verify your solution.
- Implement
Donator#transferTo(targetAccount)
according to the JavaDoc:
/**
* Transfers 1 unit of money from {@link #account} to {@code targetAccount} in a thread-safe, deadlock-free manner.
* Sleeps for 100ms after every transfer.
*/
private void transferTo(Account targetAccount) {
// FIXME
}
- Implement
BankSimulator#getBalances()
according to the JavaDoc:
/**
* Returns balances of all {@link #accounts} in a thread-safe, deadlock-free manner.
*/
List<Integer> getBalances() {
return new ArrayList<>(); // FIXME
}
- Implement
BankSimulator#isRunning()
according to the JavaDoc:
/**
* Returns {@code false} when all transfers have completed ({@code true} otherwise) in a thread-safe, deadlock-free manner.
*/
public boolean isRunning() {
return RandomUtils.nextInt(0, 10) < 9; // FIXME
}
-
Tests must pass.
-
Don’t use
exit()
orhalt()
to stop the program. -
Transfers must run concurrently if they have no accounts in common!!!
- During transfers lock on the source and target accounts but avoid deadlocks. Think how you can always guarantee that any pair of accounts is locked in the same order.
- Consider using
ReadWriteLock
inDonator#transferTo
andBankSimulator#getBalances
to stop all transfers while reading balances. Remember that read lock cannot be acquired while write lock is being held. Hint: the solution is a bit counter-intuitive :) - To implement the requirements you might need to modify existing code as well.
- Passing
BankSimulatorTest
does not give you a 100% guarantee that the code is thread-safe. - If you still want to build the application without fixing the tests, then you can do that by skipping them in the build by adding
-DskipTests
to the command:
./mvnw clean package -DskipTests
java -jar target/jf-homework7.jar 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 400
20 20 20 20 22 20 20 19 20 20 21 19 19 20 22 20 19 20 19 20 400
20 19 19 21 25 23 19 18 20 20 20 18 19 20 21 21 19 20 18 20 400
19 19 20 21 27 23 19 17 19 19 19 19 18 21 20 21 19 22 18 20 400
19 19 20 22 26 22 20 17 18 22 18 18 17 21 19 23 19 22 19 19 400
21 19 21 22 25 21 22 16 19 22 17 18 17 22 18 22 18 21 20 19 400
21 19 20 22 25 20 22 15 20 23 20 17 18 22 18 22 17 20 20 19 400
21 19 19 25 24 21 21 16 19 22 19 18 19 21 17 21 17 19 23 19 400
20 18 20 26 25 21 21 15 19 23 21 18 19 20 16 24 16 18 22 18 400
19 18 19 25 25 21 20 15 19 22 22 17 19 20 20 24 15 20 21 19 400
19 18 22 24 25 21 20 14 20 22 21 16 18 21 22 23 15 20 20 19 400
18 17 22 24 25 21 20 16 22 21 20 18 18 21 21 22 14 19 21 20 400
17 16 22 23 24 21 23 17 22 22 19 17 20 22 20 21 14 20 20 20 400
18 19 22 23 23 21 23 17 22 22 19 16 19 21 19 20 16 20 21 19 400
20 19 21 22 22 20 22 17 21 22 19 17 19 22 19 22 17 20 20 19 400
20 19 20 22 21 20 22 18 20 21 18 18 18 22 20 22 20 20 19 20 400
21 20 20 22 20 20 21 18 20 22 18 19 17 21 20 21 20 20 21 19 400
20 19 21 22 19 21 21 20 20 21 20 18 20 20 19 21 19 19 21 19 400
20 19 21 21 19 21 20 21 21 20 19 19 20 20 19 21 19 21 21 18 400
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 400
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 400
java -jar target/jf-homework7.jar 10
10 10 10 10 10 10 10 10 10 10 100
9 11 9 9 10 11 11 9 11 10 100
9 10 10 11 11 10 12 8 10 9 100
9 9 9 13 12 10 11 8 10 9 100
8 8 10 13 11 11 10 9 10 10 100
10 7 11 13 10 10 9 9 10 11 100
10 6 11 12 10 9 10 11 9 12 100
10 7 10 11 10 10 9 11 11 11 100
11 9 10 10 9 11 9 10 11 10 100
10 10 10 10 10 10 10 10 10 10 100
10 10 10 10 10 10 10 10 10 10 100
For your convenience, we have set up the Maven project to ZIP up all files in your project folder so it is easy for you to attach it to an e-mail and send it our way. All you need to do is to execute the following command in your project folder:
./mvnw clean deploy
It will ask you for your full name, Student Book Number (also known as matrikli number) and a comment (optional).
Example:
./mvnw clean deploy
#...skipping building, testing and packaging output from Maven...
[INFO] --- maven-antrun-plugin:1.8:run (package homework ZIP) @ homework7 ---
[INFO] Executing tasks
main:
Your full name (e.g. John Smith):
Jane Smith
Your Student Book Number (matrikli number, e.g. B12345):
B12345
Comment:
Java IO
[zip] Building zip: /Users/jane/workspace/jf-hw-money-transfers/target/jf-homework7-B12345.zip
[delete] Deleting: /Users/jane/workspace/jf-hw-money-transfers/homework.properties
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.041 s
[INFO] Finished at: 2017-02-03T11:35:11+02:00
[INFO] Final Memory: 21M/283M
[INFO] ------------------------------------------------------------------------
After Maven has finished, you can find the generated ZIP file in target folder with name such as jf-homework7-B12345.zip (it contains your Student Book Number/matrikli number and homework number).
The only thing left to do now is to send the ZIP file as an attachment to an e-mail with subject "Homework 7 - your Student Book Number/maktrikli number" to [email protected].