Home > Back-end >  Multithreaded primary problem.
Multithreaded primary problem.

Time:10-09

The class Demo extends Object
{
Public void finalize ()
{
System. The out. Println (" garbage collector runs a ");
}
}

The class ThreadDemo
{
Public static void main (String [] args)
{
The new Demo ();
The new Demo ();
The new Demo ();
System. The gc ();
System. Out.println (" hello world! ");
}
}

Don't quite understand why System. The gc () invokes the Demo class inside of the finalize () function, rather than the object class in the finalize () function, the elder give advice or comments, please. Thank you.

CodePudding user response:

You this question, the equivalent of the Java language system which compare the basic concepts of the rewrite (Override) don't know,,,

Suggest some primer to study, or have a look at the video learning online,

CodePudding user response:

Because your Demo class inherits the Object class, you write the finalize the Demo is rewriting the finalize of the Object class that is the way in which the Demo class annotations @ Override the finalize method, if no error means is to rewrite the method in the superclass Object, here you don't understand because Java three features in the inheritance you didn't understand, suggest read more books or video,

CodePudding user response:

First of all, any class is a subclass of the Object, write not write extends Object is always the same,
Second, finalize method is used to implement the resources of the Object cleaning method, subclasses can override this method to realize resource cleanup, GC call this method before recycling objects,
Finally, finalize the use of in general use the JNI (Java native interface) scenario, need to finalize call native methods to release a special memory, usually don't use finalize,

CodePudding user response:

You can try this
Public class A {

Public void print () {
Sout (" A ");
}
}

Public class B extends A {

Public void print () {
Sout (" B ");
}
}



The main () {
A. A=new A ();
Amy polumbo rint ();
B=new b ();
P. rint ();

}


Look at the results will understand
  •  Tags:  
  • J2ME
  • Related