Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
106 lines (85 loc) · 3.52 KB
/
Copy pathSolution.java
File metadata and controls
106 lines (85 loc) · 3.52 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
package com.javarush.task.task34.task3407;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.List;
/*
Призрачные ссылки
Разберись в примере.
Реализуй логику метода getFilledList класса Helper:
1) создай список, который сможет хранить призрачные ссылки на объекты Monkey
2) добавь в список 200 ссылок, используйте очередь helper.getQueue()
3) верни заполненный список
Требования:
1. Метод getFilledList должен возвращать список заполненный фантомными ссылками на объекты типа Monkey.
2. Метод getFilledList должен возвращать список из 200 элементов.
3. Класс Helper не должен быть приватным.
4. Метод getFilledList не должен быть приватным.*/
public class Solution {
public static Helper helper = new Helper();
public static class Monkey {
}
public static void main(String args[]) throws InterruptedException {
helper.startTime();
List<PhantomReference<Monkey>> list = helper.getFilledList();
//before GC
helper.checkListWithReferences(list, "before");
helper.callGC();
helper.heapConsuming();
//after GC
helper.checkListWithReferences(list, "after");
helper.finish();
}
public static class Helper {
private ReferenceQueue<Monkey> queue = new ReferenceQueue<>();
private long startTime;
void startTime() {
this.startTime = System.currentTimeMillis();
}
int getTime() {
return (int) (System.currentTimeMillis() - startTime) / 1000;
}
void callGC() throws InterruptedException {
System.gc();
Thread.sleep(1000);
}
public ReferenceQueue<Monkey> getQueue() {
return queue;
}
void heapConsuming() {
try {
List<Solution> heap = new ArrayList<Solution>(100000);
while (true) {
heap.add(new Solution());
}
} catch (OutOfMemoryError e) {
System.out.println("Out of memory error raised");
}
}
public void checkListWithReferences(List<PhantomReference<Monkey>> list, String string) {
int count = 0;
for (PhantomReference<Monkey> reference : list) {
if (reference.isEnqueued()) {
count++;
}
}
System.out.println(String.format("The enqueue reference count is %d (%s GC was called)", count, string));
}
public List<PhantomReference<Monkey>> getFilledList() {
ArrayList<PhantomReference<Monkey>> result = new ArrayList<>();
for(int i=0; i<200; i++){
Monkey monkey = new Monkey();
result.add(new PhantomReference<>(monkey, getQueue()));
}
return result;
}
public void finish() throws InterruptedException {
int count = 0;
while (queue.poll() != null) {
count++;
}
System.out.println(count + " objects are in the queue of phantom reference");
System.out.println("It took " + getTime() + " sec");
}
}
}