-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
159 lines (123 loc) · 5.58 KB
/
Main.java
File metadata and controls
159 lines (123 loc) · 5.58 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
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import Arquivo.Arquivo;
import Output.Output;
import Enums.TipoThread;
import Thread.MinhaThread;
import Utils.Constantes;
import Controle.*;
public class Main {
public static void main(String[] args) {
Arquivo arquivo = new Arquivo("bd.txt");
var linhas = arquivo.lerLinhas();
if (linhas.isEmpty())
throw new RuntimeException("Falha ao carregar as palavras do arquivo bd.txt");
// --- INÍCIO DO EXPERIMENTO 1: BLOQUEIO SIMPLES ---
System.out.println("--- INICIANDO TESTE: BLOQUEIO_SIMPLES ---");
Output o1 = new Output("bloqueio_simples");
if (o1.ChecarOutput() == false) {
List<String> conteudo1 = new ArrayList<>();
for (int i = 0; i <= Constantes.NUMERO_MAXIMO_THREADS; i++) {
int escritores = i;
int leitores = Constantes.NUMERO_MAXIMO_THREADS - escritores;
double tempoMedio = Rodar(escritores, leitores, linhas, "BLOQUEIO_SIMPLES");
conteudo1.add(leitores + "," + tempoMedio);
}
o1.CriarOutput(conteudo1);
System.out.println("--- TESTE BLOQUEIO_SIMPLES FINALIZADO ---");
} else {
System.out.println("Teste foi já realizado");
}
// --- INÍCIO DO EXPERIMENTO 2: LEITOR/ESCRITOR ---
System.out.println("\n--- INICIANDO TESTE: LEITOR_ESCRITOR ---");
Output o2 = new Output("leitor_escritor");
if (o2.ChecarOutput() == false) {
// (Nota: É crucial recarregar o arquivo aqui, porque o primeiro
// experimento modificou a lista 'linhas' com "MODIFICADO".
// Precisamos começar o segundo teste com a base "limpa".)
linhas = arquivo.lerLinhas();
if (linhas.isEmpty())
throw new RuntimeException("Falha ao RECARREGAR as palavras do arquivo bd.txt");
List<String> conteudo2 = new ArrayList<>();
for (int i = 0; i <= Constantes.NUMERO_MAXIMO_THREADS; i++) {
int escritores = i;
int leitores = Constantes.NUMERO_MAXIMO_THREADS - escritores;
double tempoMedio = Rodar(escritores, leitores, linhas, "LEITOR_ESCRITOR");
conteudo2.add(leitores + "," + tempoMedio);
}
o2.CriarOutput(conteudo2);
System.out.println("--- TESTE LEITOR_ESCRITOR FINALIZADO ---");
} else {
System.out.println("Teste já foi realizado");
}
}
/**
* Roda o experimento com a quantidade de leitores e escritores.
* Será impressa a duração total de todas as rodagens
*
* @param quant_escritores Auto-explicativo
* @param quant_leitores Auto-explicativo
* @param linhas Região crítica a ser acessada pelas threads
*
*/
private static double Rodar(int quant_escritores, int quant_leitores, List<StringBuffer> linhas,
String tipoControle) {
long tempoTotalGasto = 0;
for (int i = 0; i < Constantes.NUMERO_RODAGENS; i++) {
IControle controle;
if (tipoControle.equals("BLOQUEIO_SIMPLES"))
controle = new ControleBloqueioSimples();
else
controle = new ControleLeitorEscritor();
long startTime = System.currentTimeMillis();
List<MinhaThread> threads = criarArranjoDeThreads(quant_leitores, quant_escritores, linhas, controle);
List<Thread> threadsEmExecucao = new ArrayList<>();
for (Runnable thread : threads) {
Thread threadEmExecucao = new Thread(thread);
threadsEmExecucao.add(threadEmExecucao);
threadEmExecucao.start();
}
for (Thread thread : threadsEmExecucao) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
tempoTotalGasto += (endTime - startTime);
}
double tempoMedio = tempoTotalGasto / 50;
System.out.println("[" + tipoControle + "]" + " Tempo de execução com " + quant_leitores + " leitores e "
+ quant_escritores + " escritores: " + tempoMedio + " ms");
return tempoMedio;
}
/**
* Cria um arranjo de threads, embaralhado, com a quantidade de leitores e
* escritores
*
* @param quant_leitores Auto-explicativo
* @param quant_escritores Auto-explicativo
* @param linhas Região crítica a ser acessada pelas threads
* @return Arranjo embaralhado de threads
*
*/
private static List<MinhaThread> criarArranjoDeThreads(int quant_leitores, int quant_escritores,
List<StringBuffer> linhas, IControle controle) {
if (quant_escritores + quant_leitores > Constantes.NUMERO_MAXIMO_THREADS) {
throw new IllegalArgumentException(
"A soma de leitores e escritores não pode exceder " + Constantes.NUMERO_MAXIMO_THREADS);
}
List<MinhaThread> threads = new ArrayList<>();
for (int i = 0; i < quant_leitores; i++) {
threads.add(new MinhaThread(linhas, TipoThread.READER, controle));
}
for (int i = 0; i < quant_escritores; i++) {
threads.add(new MinhaThread(linhas, TipoThread.WRITER, controle));
}
// Faz-se necessário embaralhar o arranjo
Collections.shuffle(threads);
return threads;
}
}