-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBank.java
146 lines (99 loc) · 3.66 KB
/
Bank.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//package BankerAlgorithm;
import java.util.Random;
public class Bank
{
public static int requests = 0;
public static void main(String[] args) throws InterruptedException
{
int numResources = 5;
int numCustomers = 5;
int index = 0;
Random random = new Random();
int[] totalResources;
totalResources = new int[numResources];
int[] availableResources;
availableResources = new int[numResources];
System.out.print("Initial Resources Available:" + "\n[ ");
for (int i = 0; i < numResources; i++)
{
totalResources[i] = random.nextInt(9)+1;
availableResources[i] = totalResources[i];
System.out.print(totalResources[i] + " ");
}
System.out.print("]\n");
Customer[] customers;
customers = new Customer[numCustomers];
System.out.print("Bank - Max: \n");
for (int i = 0; i < numCustomers; i++)
{
customers[i] = new Customer(availableResources,i);
customers[i].start();
}
do
{
for (int i = 0; i < availableResources.length; i++)
{
if(customers[index].adequate == false){
customers[index].requestResources(availableResources, customers[index].max, customers[index].resourcesHeld);
Thread.sleep(random.nextInt(500));
index = (index + 1) % numCustomers;
}
}
do
{
for (int i = 0; i < customers.length; i++)
{
if (customers[i].adequate)
{
deallocateResources(customers[i].resourcesHeld, availableResources.length, availableResources);
}
else
{
customers[i].adequate = allocateResources(customers[i].resourcesHeld, availableResources.length, availableResources);
}
}
}while(customers[0].adequate != true && customers[1].adequate != true && customers[2].adequate != true && customers[3].adequate != true && customers[4].adequate != true);
for (int i = 0; i < customers.length; i++)
{
customers[i].adequate = false;
}
requests++;
}while (requests != 2);
System.out.print("Final available vector: \n");
System.out.print("[");
for (int i = 0; i < numResources; i++)
{
System.out.print(availableResources[i] + " ");
}
System.out.print("]\n");
}
public synchronized static boolean allocateResources (int[] request, int length, int[] availableResources)
{
boolean adequateResources = true;
for (int i = 0; i < length; i++)
{
if (availableResources[i] < request[i])
adequateResources = false;
}
if (adequateResources)
{
for (int i = 0; i < length; i++){
availableResources[i] -= request[i];
}
return adequateResources;
}
public synchronized static void deallocateResources (int[] resourcesHeld, int length, int[] availableResources)
{
System.out.print("Releasing resources: \n");
System.out.print("[");
for (int i = 0; i < length; i++)
{
System.out.print(resourcesHeld[i] + " ");
}
System.out.print("] \n");
for (int i = 0; i < length; i++)
{
availableResources[i] += resourcesHeld[i];
}
}
}