×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

2 problems .Finalize and Thread. who can answer?

HI, friends

I am studying Java. I am stuck by my another friend. he asked me 2 problems when I think I have grasped the main points of Java language. How about you?

The 2 problems are the following:

1) the following code can run. right?
class MainClass{
public static void main(String[] args){
Object o = new Object();
o.finalize();
}
}
I definitely say there is compile error, but I donot know why?

2) Thread problem
class MainClass2{
private static void main(String[] args){
Thread t = new Thread();
t = Thread.currentThread();
t = null;
System.out.println("We run");
}
}
private accessity can help main method run? why? thread t , which is main thread , has been assigned null, can the print statement be excuted? why?


Can u help me?To Jabber and other friends.

RanchHand
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 2 problems .Finalize and Thread. who can answer?
    HI, friends

    I am studying Java. I am stuck by my another friend. he asked me 2 problems when I think I have grasped the main points of Java language. How about you?

    The 2 problems are the following:

    1) the following code can run. right?
    class MainClass{
    public static void main(String[] args){
    Object o = new Object();
    o.finalize();
    }
    }
    I definitely say there is compile error, but I donot know why?

    2) Thread problem
    class MainClass2{
    private static void main(String[] args){
    Thread t = new Thread();
    t = Thread.currentThread();
    t = null;
    System.out.println("We run");
    }
    }
    private accessity can help main method run? why? thread t , which is main thread , has been assigned null, can the print statement be excuted? why?


    Can u help me?To Jabber and other friends.

    RanchHand
    • 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
      • Hi,jabber,your response is alway so helpfull for us green hands.Can you tell us something about "call back",all my know about "call back" is "system invoked function".is it right?
        • pls refer to posting #15846