×

Loading...

Let me have a try .....

本文发表在 rolia.net 枫下论坛1) In Java, one must have a concept of container. JVM is a kind of container.
All the Objects live in the container---JVM.
JVM provides an environment and system-level mangement. Without JVM, your Objects are just some words in the code.

To facilitate the system-level management,
java.lang.Object (ann corresponding ly all of its offsprings) has a method "protected void finalize()". As your Object instances are no longer needed and the memory is being depleted, JVM will collect garbage. Before garbage collection, JVM call finalize(). Bo doing so, JVM gives your Object instance an opportunity to cleanup, freeing any resources it occupied. In finalize(), you may close file, close a db collection, if any. If you havn't occupied any resources, you do nothing---it means you don't override finalize().

Finalize() is called by the container, JVM, only. We application programmer NEVER, NEVER, NEVER, NEVER call it in our code.
You code, or your friend's code, violated Java specification.

By the way, the JVM calling of finalize() method is kind of "call back".

2) You code will compile and run. You may get result "We run". But this may depend on the implementaion of JVM. The standard entry point for a Java application should have public accessibility so that anyboy can invoke it.

What I found is that you don't distinquish Object instances and their references well.

In your programm, there are two Threads: 1)
the execution of main() method ,This is mother; 2) The thread you create in your main() method, which is daughter.

Now come to your code. Your declare a variable(reference) t with a type of Thread,
then create a Thread using "new", next let "t" point to the daughter Thread. After that, you changed your mind, let "t" point to the current thread, i.e., the main method thread. Suddenly, you changed your mid again, and let "t" point to null, AS IF you point "t" to the sky. These are all you have done before printing out "We run".

Here are your wrong statement:
"thread t , which is main thread , has been assigned null". You confused Thread instances and their references(names).

A same referece can be used to point to different instances. For instance, our group has a Chinese boy which call "Lao Wang".
In a few days, we hire another Wang who is over 50. Then, by "Lao Wang" we mean that old guy. The original "Lao Wang" is still there but he is no longer called "Lao Wang".

Hope these discussion can help you.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions: