Example usage for java.lang.ref PhantomReference PhantomReference

List of usage examples for java.lang.ref PhantomReference PhantomReference

Introduction

In this page you can find the example usage for java.lang.ref PhantomReference PhantomReference.

Prototype

public PhantomReference(T referent, ReferenceQueue<? super T> q) 

Source Link

Document

Creates a new phantom reference that refers to the given object and is registered with the given queue.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReferenceQueue rq = new ReferenceQueue();
    PhantomReference<String> pr = new PhantomReference<String>("object", rq);

    while (true) {
        Reference r = rq.remove();
        if (r == pr) {
            // about to be reclaimed.
            r.clear();/* w w  w.  ja  v a 2 s . com*/
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*from   w  ww .j  a v a  2s.  c  o m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing PhantomReference.");
    reference = new PhantomReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:org.mule.module.logging.MuleLogFactory.java

public Log getInstance(String name) throws LogConfigurationException {
    final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    ConcurrentMap<String, Log> loggerMap = repository.get(ccl == null ? NO_CCL_CLASSLOADER : ccl.hashCode());

    if (loggerMap == null) {
        loggerMap = new ConcurrentHashMap<String, Log>();

        final ConcurrentMap<String, Log> previous = repository
                .putIfAbsent(ccl == null ? NO_CCL_CLASSLOADER : ccl.hashCode(), loggerMap);
        if (previous != null) {
            loggerMap = previous;/*from w w  w . j a v a 2  s.  c o  m*/
        }

        if (ccl != null) {
            // must save a strong ref to the PhantomReference in order for it to stay alive and work
            refs.put(new PhantomReference<ClassLoader>(ccl, referenceQueue), ccl.hashCode());
        }

    }

    Log instance = loggerMap.get(name);

    if (instance == null) {
        Logger logger = LoggerFactory.getLogger(name);
        if (logger instanceof LocationAwareLogger) {
            instance = new MuleLocationAwareLog((LocationAwareLogger) logger);
        } else {
            instance = new MuleLog(logger);
        }
        final Log previous = loggerMap.putIfAbsent(name, instance);
        if (previous != null) {
            // someone got there before us
            instance = previous;
        }
    }

    return instance;
}

From source file:xiaofei.library.hermes.util.HermesCallbackGc.java

public void register(IHermesServiceCallback callback, Object object, long timeStamp, int index) {
    gc();//  w w  w.ja  va  2  s.  c o m
    mTimeStamps.put(new PhantomReference<Object>(object, mReferenceQueue),
            Triple.create(callback, timeStamp, index));
}

From source file:org.apache.sling.models.impl.ModelAdapterFactory.java

private void registerCallbackRegistry(Object object, DisposalCallbackRegistryImpl registry) {
    PhantomReference<Object> reference = new PhantomReference<Object>(object, queue);
    disposalCallbacks.put(reference, registry);
}