• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sleep() and GC

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. }
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the The Ranch Jorge.
I think the reason why finalize does not complete is because the program has ended before. The threads started from the daemon g.c. thread are also daemon, thus they will not prevent the program from ending.
sleep as a way to favour g.c. ? Certainly selfish threads give no chance for others to run.
[ October 30, 2003: Message edited by: Jose Botella ]
[ October 30, 2003: Message edited by: Jose Botella ]
[ October 30, 2003: Message edited by: Jose Botella ]
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic