Calling finalize directly is just like calling any method and will occur synchronously. Although its probably a bad idea to call finalize directly as thats not what its there for. finalize is used to cleanup and release resources when a instance is released. finalize will get called at some point between when the last reference releases the instance and when the instance is garbage collected. System.runFinalization() provides a hint to the JVM that it should run any outstanding finalize methods for the released instances awaiting garbage collection. Note there is no way to know which instances finalize will be called or if in fact any are. This is analigous (big word dodgy spelling) to calling System.gc Look in the JLS section 12.6 for further information.
Joined: Jan 12, 2002
Originally posted by Phil Sharp: Calling finalize directly is just like calling any method and will occur synchronously.
Hi,Phil Thanx for explanation.But why you say calling finalize directly will make it occur "synchronously"? the signature of finalize() is protected void finalize throws Throwable() --no synchronized modifier.
Richard, Usually, the JVM is responsible for invoking that method when an object is to be garbage collected so that you have an opportunity to tidy things up in your instance. Make the difference between the synchronized keyword and synchronous which is a term for saying that a method will not be invoked until the previous one has finished executing. For instance, the start method of class Thread is not synchronous, because it returns immediately and let the Thread scheduler do its job, that is when you start a thread you get back the control almost directly (this is an asynchronous invocation). In clear, if you decide to invoke finalize by yourself, the method will behave exactly as another method, a new invocation frame will be created, the method's body will be executed and the method will return. The method invocation following the invocation to finalize will only begin executing when finalize has finished (this is synchronous invocation). HIH [ February 12, 2002: Message edited by: Valentin Crettaz ]