When to use phantom links in Java?

Possible duplicate:
Have you ever used the Phantom link in any project?

I read about different types of links. I understand how strong, soft, and weak links work.

But when I read about Phantom links, I couldn't understand them. Maybe because I could not find good examples that show me what their purpose is or when to use them.

Could you show me some code examples that use the Phantom link?

+16
java reference phantom-reference
Mar 22 '12 at 16:44
source share
1 answer

I never did this myself - very few people have ever needed this, but I think this is one way to do this.

abstract class ConnectionReference extends PhantomReference<Connection> { abstract void cleanUp(); } ... ReferenceQueue<Connection> connectionQueue = new ReferenceQueue<>(); ... Connection newConnection = ... ConnectionReference ref = new ConnectionReference(newConnection, connectionQueue, ...); ... // draining the queue in some thread somewhere... Reference<? extends Connection> reference = connectionQueue.poll(); if (reference != null) { ((ConnectionReference) reference).cleanUp(); } ... 

This is more or less similar to what this post offers.

+11
Mar 22 '12 at 17:45
source share



All Articles