-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBankingSimulator.java
48 lines (40 loc) · 1.34 KB
/
BankingSimulator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Name: Jose Tapizquent
* Course: CNT 4714 Spring 2021
* Assignment Title: Project 2 - Synchronized, Cooperating Threads Under Locking
* Due Date: February 14, 2021
*
* BankingSimulator.java
*/
import java.util.concurrent.*;
/**
* Multithreaded banking simulator composed of 5 Depositors and 9 Withdrewers
* working at the same time.
*
* Each of the actors involved perform in a different thread but are
* synchronized to work on the same account.
*
*/
public class BankingSimulator {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(14);
AccountManager accountManager = new AccountManager();
printHeader();
try {
for (int i = 0; i < 5; i++) {
executorService.execute(new Depositor(accountManager, i));
}
for (int i = 0; i < 9; i++) {
executorService.execute(new Withdrawer(accountManager, i));
}
} catch (Exception e) {
e.printStackTrace();
}
executorService.shutdown();
}
static private void printHeader() {
System.out.println("DEPOSITS\t\t\tWITHDRAWALS\t\t\t\tBALANCE");
System.out
.println("-----------------------------\t-----------------------------\t-----------------------------");
}
}