-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfinalize.java
More file actions
31 lines (23 loc) · 720 Bytes
/
finalize.java
File metadata and controls
31 lines (23 loc) · 720 Bytes
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
class Resource {
private boolean isClosed = false;
public void close() {
isClosed = true;
System.out.println("Closed normally.");
}
@Override
protected void finalize() throws Throwable {
if (!isClosed) {
System.out.println("Closed in finalize method.");
}
super.finalize();
}
}
public class finalize{
public static void main(String[] args) throws InterruptedException {
Resource resource = new Resource();
// Don't call close() method to simulate resource leak
resource = null;
System.gc(); // Request garbage collection
Thread.sleep(1000); // Wait for finalize method to be called
}
}