aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC" Watch "GC" New topic
Author

GC

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
"Only the garbage collection system can actually destroy an object."

My query is : can the GC destroy a thread ?
I consider a thread = object. so if GC can destroy an object , it can destroy a thread too.
if this is true, can anyone point where's this specifically mentioned in the JLS or anywhere.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Study this code, run it, and let us know what happens:


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Whoa....was expecting a little keyed down answer...this one hurts the brain...(groan)

well here goes:

output = Thread run done

here's how I see it:-
start()--->kicks off--> public void run()-->thread goes to sleep for 5 sec.

there's a finalize() , and afaik this will run 'once' before the stuff in the 'super' will be taken over by the GC and so I think that gc was not run because there was no output of "This has been finalized".

so, maybe the super was never 'gc'ed.....
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Did you run it? I got both messages printed.
[ December 03, 2004: Message edited by: Barry Gaunt ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
afaik this will run 'once'


What does afaik mean?


Mike Gershman
SCJP 1.4, SCWCD in process
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Oh yes, I did run it
I ran it 'once' on win2k, in dos box.
Now knowing thread behaviour are never guaranted and specially not on a win32 platform but then there was no suggestion that I run this code 'x' ammount of times to get 'the' result.

and as I write this mail, I tried the code 3 more times , all culminating in "Thread run done" [ after a slight pause...due to the 'sleep' ].
but since we are not able to see what's planned for me to see, a 'yes' or 'no' would suffice for me to know if threads can be destroyed by GC.

and oh yes....as far as I know[afaik]
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
and continuing...

since you got both messages printed, that shall answer my question.

Thanks all...

**PS:-but I would still love to see if this was literally worded someplace...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Mike Gershman:


What does afaik mean?


"as far as I know"
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Barry Gaunt:
Did you run it? I got both messages printed.

[ December 03, 2004: Message edited by: Barry Gaunt ]


To add to the "confusion": the two lines came in the order:
"This has been finalized"
"Thread run done"

But I was able to prove to myself that the thread had really finished its run method before the finalized message was printed.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Correct me if I'm wrong here Barry [ I mostly am ]

GC goes through a semblence of running when it sniffs the programme's running out of memory [ well it may never run not even with the "System.gc();" cmd., as a matter of fact ].
My machine where I tried this upon is welllllll.... ' kinda souped up ', I've a over a Gig. memory. so maybe running this code snippet and doing some other overheads on this machine may be termed 'peanuts' in memory usurp terms.
once again, maybe pulling off some RAM from this box, may show me the 2 lines too...

It's all and remains a "maybe" after all....but that was just a thought.

well thanx again for dwelling onto this 'yet another kranky one from me'

[ December 03, 2004: Message edited by: Netty poestel ]
[ December 03, 2004: Message edited by: Netty poestel ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
No problem, it was an interesting question. I should be doing something else here at work, but that one kept me entertained for a while.

Sometimes the System.gc() calls can do the trick, but not on all implementations.

PS It appears that I get the same result as you when I use the DOS command, my previous results were in the BlueJ IDE.

The interesting thing is that the JVM must keep an internal (hidden) reference to the thread object to prevent it being GCed while it is still in its run method.

[ December 03, 2004: Message edited by: Barry Gaunt ]
[ December 03, 2004: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC