-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanheiro.java
211 lines (174 loc) · 5.11 KB
/
banheiro.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
class BanheiroMonitor {
public static int VAZIO = 0;
public static int COM_MULHER = 1;
public static int COM_HOMEM = 2;
public static int MAX_PESSOAS = 2;
public int qtdPessoasNoBanheiro = 0;
public int state;
BanheiroMonitor() {
this.state = BanheiroMonitor.VAZIO;
}
public synchronized void homem_quer_entrar(int homemId) {
if( this.state == BanheiroMonitor.COM_MULHER ) {
this.go_to_sleep();
}
if( this.qtdPessoasNoBanheiro == BanheiroMonitor.MAX_PESSOAS) {
this.go_to_sleep();
}
if( this.state == BanheiroMonitor.VAZIO ) {
System.out.println("BANHEIRO COM HOMEM");
}
this.state = BanheiroMonitor.COM_HOMEM;
this.qtdPessoasNoBanheiro += 1;
System.out.println("HOMEM " + homemId + " entrou no banheiro");
}
public synchronized void homem_sai(int homemId) {
System.out.println("HOMEM " + homemId + " saiu do banheiro");
this.qtdPessoasNoBanheiro -= 1;
if ( this.qtdPessoasNoBanheiro == 0 ) {
this.state = BanheiroMonitor.VAZIO;
System.out.println("BANHEIRO VAZIO");
notify();
}
}
public synchronized void mulher_quer_entrar(int mulherId) {
if( this.state == BanheiroMonitor.COM_HOMEM ) {
this.go_to_sleep();
}
if( this.qtdPessoasNoBanheiro == BanheiroMonitor.MAX_PESSOAS) {
this.go_to_sleep();
}
if( this.state == BanheiroMonitor.VAZIO ) {
System.out.println("BANHEIRO COM MULHER");
}
this.state = BanheiroMonitor.COM_MULHER;
this.qtdPessoasNoBanheiro += 1;
System.out.println("MULHER " + mulherId + " entrou no banheiro");
}
public synchronized void mulher_sai(int mulherId) {
System.out.println("MULHER " + mulherId + " saiu do banheiro");
this.qtdPessoasNoBanheiro -= 1;
if ( this.qtdPessoasNoBanheiro == 0 ) {
this.state = BanheiroMonitor.VAZIO;
System.out.println("BANHEIRO VAZIO");
notify();
}
}
private void go_to_sleep(){
try{
wait();
} catch(InterruptedException e){
System.out.println("Erro ao executar go_to_sleep " + e.getMessage());
}
}
}
class RandomNumber {
public static int generate(int min, int max) {
Random rand = new Random();
return rand.nextInt((max - min) + 1) + min;
}
}
class GeradorHomens extends Thread {
private int quantidadeDeHomens;
private BanheiroMonitor banheiroMonitor;
private Homem[] homens;
GeradorHomens(int quantidadeDeHomens, BanheiroMonitor banheiroMonitor) {
this.quantidadeDeHomens = quantidadeDeHomens;
this.banheiroMonitor = banheiroMonitor;
this.homens = new Homem[quantidadeDeHomens];
}
public void run() {
Timer timer = new Timer();
GeradorHomens $this = this;
timer.schedule(new TimerTask() {
public void run() {
for(int i = 0; i < quantidadeDeHomens; i++) {
$this.homens[i] = new Homem(i, banheiroMonitor);
$this.homens[i].run();
}
timer.cancel();
}
}, RandomNumber.generate(1, 4));
}
}
class Homem extends Thread {
private int id;
private BanheiroMonitor banheiroMonitor;
private Timer timerHomem = new Timer();
Homem(int id, BanheiroMonitor banheiroMonitor) {
this.id = id;
this.banheiroMonitor = banheiroMonitor;
}
public void run() {
Homem $this = this;
banheiroMonitor.homem_quer_entrar(this.id);
timerHomem.schedule(new TimerTask() {
public void run() {
banheiroMonitor.homem_sai($this.id);
$this.timerHomem.cancel();
}
}, 1);
}
}
class GeradorMulheres extends Thread {
private int quantidadeDeMulheres;
private BanheiroMonitor banheiroMonitor;
private Mulher[] mulheres;
GeradorMulheres(int quantidadeDeMulheres, BanheiroMonitor banheiroMonitor) {
this.quantidadeDeMulheres = quantidadeDeMulheres;
this.banheiroMonitor = banheiroMonitor;
this.mulheres = new Mulher[quantidadeDeMulheres];
}
public void run() {
Timer timer = new Timer();
GeradorMulheres $this = this;
timer.schedule(new TimerTask() {
public void run() {
for(int i = 0; i < quantidadeDeMulheres; i++) {
$this.mulheres[i] = new Mulher(i, banheiroMonitor);
$this.mulheres[i].run();
}
timer.cancel();
}
}, 1);
}
}
class Mulher extends Thread {
private int id;
private BanheiroMonitor banheiroMonitor;
private Timer timerMulher = new Timer();
Mulher(int id, BanheiroMonitor banheiroMonitor) {
this.id = id;
this.banheiroMonitor = banheiroMonitor;
}
public void run() {
Mulher $this = this;
banheiroMonitor.mulher_quer_entrar(this.id);
timerMulher.schedule(new TimerTask() {
public void run() {
banheiroMonitor.mulher_sai($this.id);
$this.timerMulher.cancel();
}
}, 2);
}
}
public class banheiro {
public static void main(String args[]) {
BanheiroMonitor banheiro = new BanheiroMonitor();
int quantidadeDeHomens = Integer.parseInt(args[0]);
int quantidadeDeMulheres = Integer.parseInt(args[1]);
GeradorHomens geradorDeHomens = new GeradorHomens(quantidadeDeHomens, banheiro);
GeradorMulheres geradorDeMulheres = new GeradorMulheres(quantidadeDeMulheres, banheiro);
geradorDeHomens.run();
geradorDeMulheres.run();
try {
geradorDeHomens.join();
geradorDeMulheres.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}