Hi, this is the class and bottom the comments:
class
Test extends Thread{
public Test(){
System.out.println("Constructor Test");
}
public void run(){
for(int i=0;i<3;i++){
System.out.println("Run and sleep " + i);
try{
sleep(100); //without this sleep() the GC wait more time to clean up
}
catch(InterruptedException e){System.out.println("Error");}
}//end for
new Fini().start();
}
public void finalize(){
System.out.println("Test finalize");
Test t2 = new Test();
t2.start();
}
}
class Fini extends Thread{
public Fini(){
System.out.println("Constructor Fini");
}
public void run(){
System.gc();// System.runFinalization();
System.out.println("Call to gc");
}
}
public class Zero{
public static void main(
String [] args){
Test t = new Test();
t.start();
}
}
Print something like that :
Constructor Test
Run and sleep 0
Run and sleep 1
Run and sleep 2
Constructor Fini
Call to gc
Test finalize
Constructor Test
Run and sleep 0
In the last line, the second object dont finish the loop-for because the
sleep give the schedule to the Garbage Collector to clean the first object
who is the link to the second object.
So the sleep() can give a chance to the GC, if i am wrong please correct me.
else {thank to the javaranch friends. }