How does the "Java2D Disposer"-Thread work?

Hi again,

I am currently building some java2d/swing based network interface where both on the client/server objects are referenced on their counterparts through an unique ID. The problem is that the HashMap where I store those IDs is constantly growing of course.

So I could either introduce dispose() - or better implement something that works like the “Java2D Disposer”-Thread which seems to know how to handle Peers which don’t have their java-counterpart anymore.

Does anybody know articles about how this works?

Thank you in advance, lg Clemens

Well, with Java being open sourced you can just go and take a look yourself.
It’s in j2se/src/share/classes/sun/java2d/Disposer.java and
src/share/native/sun/java2d/Disposer.c.

Basically it uses WeakReferences and a Reference queue. When
an object WeakReference gets collected, the disposer gets it
from the queue and disposes whatever resources were
associated with the object.

Dmitri
Java2D Team

Thanks a lot for the detailed reply, yes I already checked the source and it contains less “magic” than I thought.
Its always amazing how clean Sun’s code is written without doing fancy things. Most stuff is solved using public APIs, very clean and readable :slight_smile:

I implemented it now quite the same way but using PhantomReferences to avoid resurrection by finalizers. Works great so far :slight_smile:

Thanks, lg Clemens