-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
127 lines (91 loc) · 3.89 KB
/
Solution.java
File metadata and controls
127 lines (91 loc) · 3.89 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
//And when I saw him, I fell at his feet as dead. And he laid his right hand upon me, saying unto me, Fear not; I am the first and the last (Revelation 1:17)
package com.javarush.task.task34.task3407;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.ArrayList;
import java.util.List;
/*
Призрачные ссылки
*/
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() {
//создаем 200 объектов и добавляем их в список через призрачные ссылки
ArrayList<PhantomReference<Monkey>> result = new ArrayList<PhantomReference<Monkey>>();
for ( int i = 0; i < 200; i++) {
Monkey monkey = new Monkey();
result.add(new PhantomReference (monkey, queue));
}
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");
}
}
}
/*
Призрачные ссылки
Разберись в примере.
Реализуй логику метода getFilledList класса Helper:
1) создай список, который сможет хранить призрачные ссылки на объекты Monkey
2) добавь в список 200 ссылок, используйте очередь helper.getQueue()
3) верни заполненный список
Требования:
1. Метод getFilledList должен возвращать список заполненный фантомными ссылками на объекты типа Monkey.
2. Метод getFilledList должен возвращать список из 200 элементов.
3. Класс Helper не должен быть приватным.
4. Метод getFilledList не должен быть приватным.
*/