Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

GC doubt

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Select three correct statements

A. The garbage collection thread will die just after the last user thread dies.
B. The garbage collection can be forced by invoking System.gc().
C. The garbage collection thread is a non-deamon thread.
D. The finalize() method is invoked at most once by the JVM for any given object.
E. The finalize() method may resurrect the object upon which it has been invoked.

Correct answers are A,D,E

I have doubt in D. According to me finalize() method may run or not on a particular object based upon it the above option "D" is wrong. Correct me if i am wrong

Source: Source


Thanks All
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi nick,

at most once - doesn't imply must run for once.

it says about max no of times it can get called.

it doesn't speak about min no of times.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have doubt in D. According to me finalize() method may run or not on a particular object based upon it the above option "D" is wrong. Correct me if i am wrong



"may or may not run once" == "at most once(0 or 1)" == "option D is right"
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> The finalize() method may resurrect the object upon which it has been invoked.
>> The finalize() method is invoked at most once by the JVM for any given object.

So if reference to this ressurected object is lost, then finalize is not called again?
Every object can be resurrected only once?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can you please explain option E.

Thanks
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sarika kumar:
Hi,
Can you please explain option E.

Thanks



you save the reference (this) in finalize and make a copy of it.

 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Stone:
>> The finalize() method may resurrect the object upon which it has been invoked.
>> The finalize() method is invoked at most once by the JVM for any given object.

So if reference to this ressurected object is lost, then finalize is not called again?
Every object can be resurrected only once?



my experiments are saying YES

javadoc says:
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had a related query. I an object is resurrected and again it becomes eligible for garbage collection, is there a way to do something just before its garbage collected (something like finalize method), and where is this count of how many times an object has been resurrected maintained.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everybody for help
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Posted by Satya

an object is resurrected and again it becomes eligible for garbage collection, is there a way to do something just before its garbage collected (something like finalize method), and where is this count of how many times an object has been resurrected maintained.



Why would you need it, when you know the anser ?
It will be at most once.

When an object is eligible for garbage collection, it can be resurrected via the finalize method (which will run at most once by the GC).

The second time the object is elgible for GC, the finalize method won't run, hence it can't be resurrected and the object will be garbage collected.
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys,

This is a bit of a hijack - sorry.

I looked over some of the questions from the "Source". The quality of the questions is okay, but I saw quite a few questions that were out of scope for the actual exam! The test is hard enough even when you focus on the correct topics, so I'd advise you all to study the actual Sun oblectives very carefully and make sure that you don't waste time on mock questions that are out of scope.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bert,
Ya actually there are some questions which are out of scope for the exam and thanks bert for the reply and i am weak in threads generally in the questions where two threads start at a time and run. I cannot predict the output can you help me?
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From K&B:


(i) When it comes to threads, very little is guaranteed.
(ii) The order in which runnable threads are chosen to run is not guaranteed.


[ May 10, 2007: Message edited by: M Krishnan ]
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks All for your help
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic