Home > Back-end >  Java destructor
Java destructor

Time:09-16

I write in the operation process of a program in JNA, class destructor when need to use CloseHandle () function to close the process handle
So write about
 
The class Process {
Private HANDLE HANDLE
Public Process (HANDLE h) {
Handle=h;
}
.
@ Override
Public void finalize () {
The CloseHandle (handle);
}
}

Use:
 
//method in the main ()
The Process of p=new Process (handle);
.
P=null;
System. The gc ();

But this feeling is a bit of trouble, is there any more convenient method?

CodePudding user response:

Direct call CloseHandle (handle); Will be fine, don't write the finalize (), this is no guarantee that will be executed, jdk10 + has been abandoned by this method

CodePudding user response:

Don't do it, and want to be in a reliable, specific code trigger points to close the handle, Java finalize () concept is a concept has been eliminated, don't use it to do anything,
  • Related