Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

GC

 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Study this code, run it, and let us know what happens:
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you run it? I got both messages printed.
[ December 03, 2004: Message edited by: Barry Gaunt ]
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
afaik this will run 'once'


What does afaik mean?
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Gershman:


What does afaik mean?


"as far as I know"
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic