-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
103 lines (67 loc) · 2.56 KB
/
Customer.java
File metadata and controls
103 lines (67 loc) · 2.56 KB
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
//package BankerAlgorithm;
import java.util.*;
public class Customer extends Thread
{
public int[] max;
public int[] resourcesHeld;
public int Id;
public boolean adequate = false;
public Customer (int[] availableResources, int CustomerId)
{
Random random = new Random();
Id = CustomerId;
max = new int[availableResources.length];
resourcesHeld = new int[availableResources.length];
System.out.print("[ ");
for (int i = 0; i < availableResources.length; i++)
{
max[i] = random.nextInt(availableResources[i])+1;
resourcesHeld[i] = 0;
System.out.print(max[i] + " ");
}
System.out.println("]");
}
public synchronized void requestResources(int[] availableResources, int[] max, int[] resourcesHeld)
{
Random random = new Random();
int[] request;
request = new int[availableResources.length];
for (int i = 0; i < availableResources.length; i++)
{
request[i] = random.nextInt(max[i]);
}
System.out.print("Customer " + Id + " making a request: \n");
System.out.print("[");
for (int i = 0; i < request.length; i++)
{
System.out.print(request[i]+" ");
}
System.out.print("]");
System.out.println();
System.out.print("Available resources are: \n");
System.out.print("[");
for (int i = 0; i < request.length; i++)
{
System.out.print(availableResources[i]+" ");
}
System.out.print("]");
System.out.println();
if (Bank.allocateResources(request, request.length, availableResources))
{
adequate = true;
System.out.print("Bank - Safe Sequence: [0, 1, 2, 3, 4] \n");
System.out.print("Customer " + Id + " Request " + Bank.requests + " granted.\n");
for (int i = 0; i < availableResources.length; i++)
{
resourcesHeld[i] = request[i];
max[i] -= request[i];
}
}
else
{
System.out.print("Bank - Safe sequence not found");
System.out.print("Customer " + Id + " request denied. Not enough available resources. Customer " + Id + " must wait. \n");
Thread.yield();
}
}
}