У меня есть PR, объект O, на который указывает PR, и RQ, настроенный для PR. У меня есть поток, который продолжает опрашивать RQ, и при первой ссылке, которую он находит в RQ, поток печатает время, когда он нашел ее, и завершает работу.
Все работает нормально, но в тот момент, когда O завершает работу (независимо от того, насколько это тривиально), поток больше не находит ссылку в RQ и продолжает работать бесконечно.
Вопрос: почему это происходит таким образом? Я использую Sun JDK 1.6.
Вот код:
public class MyGCPhantom
{
public static void main(String[] args) throws InterruptedException
{
GCPhantomObject p = new GCPhantomObject();
ReferenceQueue phantomQueue = new ReferenceQueue();
PhantomReference pr =
new PhantomReference(p, phantomQueue);
new GCPhantomThread(phantomQueue, "Phantom").start();
p = null;
System.gc();
}
}
class GCPhantomObject
{
@Override
protected void finalize()
{
// uncomment the line below to trigger the issue
// System.out.println("GCPO finalized " + System.currentTimeMillis());
}
}
class GCPhantomThread extends Thread
{
private ReferenceQueue referenceQueue;
private String name;
GCPhantomThread(ReferenceQueue referenceQueue, String name)
{
this.referenceQueue = referenceQueue;
this.name = name;
}
@Override
public void run()
{
while(referenceQueue.poll() == null);
System.out.println(name + " found at " + System.currentTimeMillis());
}
}
Подробнее здесь: https://stackoverflow.com/questions/129 ... es-to-it-f