简体   繁体   English

是否有任何钩子可以避免在Java中用垃圾回收注册线程?

[英]Is there any hook provoided to register a thread with garbage collection in java?

JVM provides you a hook to register a thread with the shutdown initiation sequence. JVM为您提供了一个使用关闭启动序列注册线程的钩子。 Once a thread is registered, on every shutdown that thread is run. 注册线程后,在每次关闭时都会运行该线程。

Now, is there any such a hook java provide to register a thread with JVM's Garbage collector? 现在,java是否提供了这样的钩子来向JVM的Garbage收集器注册线程?

You do have finalizers , but they're not guaranteed to run. 您确实有终结器 ,但不能保证它们会运行。

Further discussion about why finalizers may not run in this thread: When is the finalize() method called in Java? 关于终结器为何不能在此线程中运行的进一步讨论: Java中何时调用finalize()方法?

不完全是一个钩子,但是您可以使用WeakReference来通知对象已经可以进行垃圾收集了。

There are WeakReferences as already mentioned but there are also Phantom References 如前所述,有WeakReferences,但也有Phantom References。

All of these techniques just allow you to monitor garbage collection on specific objects though. 所有这些技术仅允许您监视特定对象上的垃圾收集。

There is a good description of phantom references here: http://java.dzone.com/articles/finalization-and-phantom 这里对幻像引用有很好的描述: http : //java.dzone.com/articles/finalization-and-phantom

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM