-
Notifications
You must be signed in to change notification settings - Fork 17
/
AddMultithreading.java
127 lines (109 loc) · 3.53 KB
/
AddMultithreading.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
package by.andd3dfx.multithreading;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
/**
* <pre>
* Модифицировать данный код, чтобы добавить многопоточность при выполнении запросов в systemA и systemB.
*
* public class Aggregator {
* private SystemA systemA;
* private SystemB systemB;
*
* Object doRequest() { // 10 sec
* Object responseA = systemA.doRequest(); // 5 sec
* Object responseB = systemB.doRequest(); // 5 sec
* return aggregate(responseA, responseB);
* }
*
* Object aggregate(Object responseA, Object responseB) {
* // ...
* return data;
* }
* }
*
* class SystemA {
* Object doRequest() {
* // ...
* return data;
* }
* }
*
* class SystemB {
* Object doRequest() {
* // ...
* return data;
* }
* }
* </pre>
*
* @see <a href="https://youtu.be/3IGsZy0uTSk">Video solution</a>
*/
public class AddMultithreading {
public static class Aggregator {
private SystemA systemA;
private SystemB systemB;
private SystemA[] aSystems;
private SystemB[] bSystems;
public Aggregator(SystemA systemA, SystemB systemB) {
this.systemA = systemA;
this.systemB = systemB;
}
public Aggregator(SystemA[] aSystems, SystemB[] bSystems) {
this.aSystems = aSystems;
this.bSystems = bSystems;
}
public Object doRequestOld() {
Object responseA = systemA.doRequest();
Object responseB = systemB.doRequest();
return aggregate(responseA, responseB);
}
@SneakyThrows
public Object doRequest() {
var futureA = CompletableFuture.supplyAsync(() -> systemA.doRequest());
var futureB = CompletableFuture.supplyAsync(() -> systemB.doRequest());
return futureA
.thenCombine(futureB, Aggregator::aggregate)
.get();
}
@SneakyThrows
public Object doRequest10() {
var futures = new ArrayList<CompletableFuture<Object>>();
for (var system : aSystems) {
futures.add(CompletableFuture.supplyAsync(() -> system.doRequest()));
}
for (var system : bSystems) {
futures.add(CompletableFuture.supplyAsync(() -> system.doRequest()));
}
var resultFuture = CompletableFuture.supplyAsync(() -> "");
for (var future : futures) {
resultFuture = resultFuture.thenCombine(
future, Aggregator::aggregate
);
}
return resultFuture.get();
}
private static String aggregate(Object responseA, Object responseB) {
return "%s%s".formatted(responseA, responseB);
}
}
@AllArgsConstructor
public static class SystemA {
private String data;
@SneakyThrows
Object doRequest() {
Thread.sleep(1_000);
return data;
}
}
@AllArgsConstructor
public static class SystemB {
private String data;
@SneakyThrows
Object doRequest() {
Thread.sleep(1_000);
return data;
}
}
}