-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMemoryEaterEvelyn.java
71 lines (59 loc) · 1.9 KB
/
MemoryEaterEvelyn.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
package memory;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import static memory.Utils.inputAwait;
public class MemoryEaterEvelyn {
@SneakyThrows
public static void main(String[] args) {
inputAwait();
var counter = new AtomicLong();
var eventList = new ConcurrentHashMap<Long, Event>();
new Thread(new EvilEvelyn(counter, eventList)).start();
for (int i = 0; i < 10; i++) {
new Thread(new HappyEvelyn(counter, eventList)).start();
}
while (true) {
// Just wait
Thread.sleep(500L);
System.out.println("Cakes eaten: " + counter.get());
}
}
@RequiredArgsConstructor
private static class HappyEvelyn implements Runnable {
private final AtomicLong counter;
private final Map<Long, Event> eventList;
@Override
@SneakyThrows
public void run() {
while (true) {
var cake = new MemoryCake();
var idx = this.counter.incrementAndGet();
eventList.put(idx, new MyEvent());
Thread.sleep(10L);
}
}
private static class MyEvent extends Event {
}
}
@RequiredArgsConstructor
private static class EvilEvelyn implements Runnable {
private final AtomicLong counter;
private final Map<Long, Event> eventList;
@Override
@SneakyThrows
public void run() {
while (true) {
var cake = new MemoryCake();
var idx = this.counter.incrementAndGet();
eventList.put(idx, new Event() {});
Thread.sleep(10L);
}
}
}
@RequiredArgsConstructor
private static abstract class Event {
}
}