-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
145 lines (128 loc) · 5.14 KB
/
Main.java
File metadata and controls
145 lines (128 loc) · 5.14 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
import java.util.*;
import Elements.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Collections;
import Structure.Words;
public class Main {
public static void main(String[] args) {
try {
Thread semExclusividade = new Thread(Main::runSimultaneo);
semExclusividade.start();
Thread comExclusividade = new Thread(Main::runExclusivo);
comExclusividade.start();
semExclusividade.join();
comExclusividade.join();
}catch(Exception ex) {
System.out.println("Erro ao ler o arquivo: " + ex.getMessage());
}
}
public static void runSimultaneo() {
try {
List<Element> runnables = new ArrayList<>();
Words palavras = new Words(LerBancoDePalavras());
// Testar diferentes proporções
for (int i = 0; i <= 100; i += DIFFERENCE) {
runnables.clear();
int readerCount = i;
int writerCount = 100 - readerCount;
for (int j = 0; j < readerCount; j++) {
Element reader = new Reader(palavras, true);
runnables.add(reader);
}
for (int k = readerCount; k < 100; k++) {
Element writer = new Writer(palavras);
runnables.add(writer);
}
long sum = 0;
for (int m = 0; m < 50; m++) {
// Ordenar aleatório
Collections.shuffle(runnables);
List<Thread> threads = new ArrayList<>();
long startTime = System.currentTimeMillis();
// Colocar cada elemento na Thread
for (Element e : runnables) {
Thread t = new Thread(e);
t.start();
threads.add(t);
}
for (Thread t : threads) {
t.join();
}
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
sum += duration;
}
double avg = (double)sum / 50.0;
try (PrintWriter escritor = new PrintWriter(new FileWriter("./Outputs/runSimultaneo.txt", true))) {
escritor.append(readerCount+","+writerCount+","+avg+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static int DIFFERENCE = 1;
public static void runExclusivo(){
List<Element> runnables = new ArrayList<>();
try {
Words palavras = new Words(LerBancoDePalavras());
// Testar diferentes proporções
for (int i = 0; i <= 100; i += DIFFERENCE) {
runnables.clear();
int readerCount = i;
int writerCount = 100 - readerCount;
for (int j = 0; j < readerCount; j++) {
Element reader = new Reader(palavras, false);
runnables.add(reader);
}
for (int k = readerCount; k < 100; k++) {
Element writer = new Writer(palavras);
runnables.add(writer);
}
long sum = 0;
for (int m = 0; m < 50; m++) {
// Ordenar aleatório
Collections.shuffle(runnables);
List<Thread> threads = new ArrayList<>();
long startTime = System.currentTimeMillis();
// Colocar cada elemento na Thread
for (Element e : runnables) {
Thread t = new Thread(e);
t.start();
threads.add(t);
}
for (Thread t : threads) {
t.join();
}
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
sum += duration;
}
double avg = (double)sum / 50.0;
try (PrintWriter escritor = new PrintWriter(new FileWriter("./Outputs/runExclusivo.txt", true))) {
escritor.append(readerCount+","+writerCount+","+avg+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static List<String> LerBancoDePalavras() throws FileNotFoundException {
File arquivo = new File("./bd.txt");
Scanner sc = new Scanner(arquivo);
List<String> palavras = new ArrayList<>();
while (sc.hasNext()) {
palavras.add(sc.nextLine());
}
sc.close();
return palavras;
}
}