
In order to test this method, I wrote the following test code:
package basic;
public class finalizeTest {
private String name;
public void finalize(){
System.out.println("finalize called: " + this.name);
System.out.println("Thread id in finalize: " + Thread.currentThread().getId());
}
public finalizeTest(String name){
this.name = name;
}
public static void main(String[] args) {
Object object = null;
System.out.println("Thread id: " + Thread.currentThread().getId());
finalizeTest test = new finalizeTest("Jerry");
new finalizeTest("Scala");
System.gc();
}
}
The code gives the following output:
Thread id: 1
finalize called: Scala
Thread id in finalize: 3
I have learned two points through this test program:
1. the finalize method redefined in my own class is called in a different thread:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
10 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
5 | |
5 |