Java – how to detect when objects are no longer referenced

Is there any way to create or register a handler that will be called completely the last time an object is referenced?

An example is an object supported by a physical data file. Once the object becomes unreferenced, the file should be closed instead of renamed It would be nice if this were possible without explicitly calling the "close" method on the object

All the notification mechanisms I know from the weak / ghost reference area only mean that the notification will occur at a certain point in time, but there is no notification about when it will occur

Solution

Anyway, No

The Java specification explicitly denies you the ability to know when the last reference is released JVM implementation (and Optimization) depends on this No hook

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>