-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
105 lines (71 loc) · 2.59 KB
/
Solution.java
File metadata and controls
105 lines (71 loc) · 2.59 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
//All things were made by him; and without him was not any thing made that was made. (John 1:3)
package com.javarush.task.task34.task3405;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
/*
Мягкие ссылки
*/
public class Solution {
public static Helper helper = new Helper();
public static class Monkey {
private String name;
public Monkey(String name) {
this.name = name;
}
protected void finalize() {
Helper.isFinalized = true;
System.out.format("Bye-Bye, %s!\n", name);
}
}
public static void main(String args[]) throws InterruptedException {
helper.startTime();
Monkey monkey = new Monkey("Simka");
SoftReference <Monkey> reference = new SoftReference<Monkey>(monkey);//Add reference here
helper.callGC();
monkey = null;
helper.callGC();
helper.heapConsuming();
if (reference.get() == null)
System.out.println("Finalized");
helper.finish();
}
public static class Helper {
public static boolean isFinalized;
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);
}
void heapConsuming() {
try {
List<Solution> heap = new ArrayList<Solution>(100000);
while (!isFinalized) {
heap.add(new Solution());
}
} catch (OutOfMemoryError e) {
System.out.println("Out of memory error raised");
}
}
public void finish() {
System.out.println("Done");
System.out.println("It took " + getTime() + " sec");
}
}
}
/*
Мягкие ссылки
Разберись в примере.
Внутри метода main после создания объекта типа Monkey создай мягкую ссылку (SoftReference) на него.
Требования:
1. В методе main должен быть создан объект типа Monkey с именем "Simka".
2. В методе main должен быть создан SoftReference на объект monkey.
3. Класс Monkey должен быть публичным.
4. Класс Monkey должен быть статическим.
*/