This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads and Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads and Garbage Collection" Watch "Threads and Garbage Collection" New topic
Author

Threads and Garbage Collection

Gisselle Szaritsa
Greenhorn

Joined: Jan 13, 2003
Posts: 13
Sorry for being such a pain , but I have yet another question. Does the garbage collector run in it's own thread? If so, is it considered a user thread or a daemon thread? I heard somewhere that the gc cannot outlive the last user thread - which would make sense because it would be rather silly to run the gc even after the application has ended (which occurs once the last user thread has died and the system kills off all remaining daemon threads). So, does this imply somehow that the gc is a daemon thread??? Thank you!!!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The best way to do this is to ask Java during the runtime and this is the best way you can find out things you are unsure about. Let's try out the following code:

When running this code you get the following output:

You can see that the GC thread (actually called Finalizer) is a daemon thread.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Gisselle Szaritsa
Greenhorn

Joined: Jan 13, 2003
Posts: 13
Thanks a million I hadn't realized that I could ask the runtime system itself. Thanks again partner, Gisselle
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Finalizer is not the g.c. thread. It is the thread that calls the finalize methods. It is an instance of java.lang.ref.Finalizer.FinalizerThread. In the source code for it there is no implementation of the algorithms used in a g.c.
I guess the g.c. is implemented in the same language as the JVM and is part of it.
By the way, ReferHandler is an instance of java.lang.ref.Reference.ReferenceHandler and it enques the registered Reference objects.
Hey Val, for something curious call your code within the finalize method of 10000 thousand garbage collectable objects, and call System.gc()
When the main thread dissapears, the finalizer still goes on a bit, but a thread named DestroyJavaVM shows up for a while.


SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Thank you for that useful information Jose. Now I'm screwed up
Sorry Gisselle if I have provided the wrong information.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Threads and Garbage Collection
 
Similar Threads
setDaemon method
What exactly are daemon threads?
Another JQ+ Thread
Daemon threads
thread