Example usage for java.lang.ref Reference get

List of usage examples for java.lang.ref Reference get

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public T get() 

Source Link

Document

Returns this reference object's referent.

Usage

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";
        }//w ww .j av a 2  s .  c o m
    };
    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(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:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }//from www. ja  v a2s  .  c o m
    };

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

    HashMap map = new HashMap();
    Reference reference = new SoftReference(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:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }//from   ww  w  .ja va 2s  . co m
    };

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

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing WeakReference.");
    reference = new WeakReference(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:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }//from w w  w. j ava 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:Main.java

static void printObject(Object obj) {
    System.out.println("find threadlocal var:" + obj);
    if (obj instanceof Object[]) {
        System.out.println("array:" + Arrays.deepToString((Object[]) obj));
    } else if (obj instanceof Reference) {
        Reference ref = (Reference) obj;
        System.out.println("ref: " + ref.getClass().getName() + ",ref to " + ref.get());
    } else {//from  ww  w.j av a 2  s  .co m
        System.out.println("others: " + obj);
    }
}

From source file:Main.java

private static BufferedImage getPooledImage(int width, int height, int index) {
    String key = String.format("%dx%d#%d",
            new Object[] { Integer.valueOf(width), Integer.valueOf(height), Integer.valueOf(index) });
    Reference ref = (Reference) imagePool.get(key);
    BufferedImage image = ref == null ? null : (BufferedImage) ref.get();

    if (image == null) {
        image = new BufferedImage(width, height, 2);
        imagePool.put(key, new SoftReference(image));
    }/*w ww  . j  a  va 2 s .  c o  m*/

    return image;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
private static void printObject(ThreadLocal threadLocal, Object obj) {
    String threadLocalCls = threadLocal.getClass().getName();
    if (threadLocal instanceof NamedThreadLocal) {
        System.out.print("......Spring threadlocal var: " + threadLocal + "=");
    } else if (obj == null || threadLocalCls.startsWith("java") || threadLocalCls.startsWith("sun")) {
        return;/*from  www.  ja va2s . c om*/
    }
    if (obj instanceof Object[]) {
        System.out.println(Arrays.deepToString((Object[]) obj));
    } else if (obj instanceof java.lang.ref.Reference) {
        java.lang.ref.Reference ref = (Reference) obj;
        System.out.println(" ref " + ref.getClass().getName() + " ref to " + ref.get());
    } else if (obj instanceof ThreadLocal) {
        ThreadLocal threadLc = (ThreadLocal) obj;
        String clssName = threadLc.getClass().getName();
        System.out.println(threadLc.getClass().getName() + "=" + threadLc.get());
    } else {
        System.out.println(obj);
    }
}

From source file:net.femtoparsec.jnlmin.utils.PropertySetter.java

public static PropertySetter getPropertySetter(Class beanClass) {
    cleanCache();/*from  w w  w  .j  a  v  a2  s  .co m*/

    Reference<PropertySetter> reference = CACHE.get(beanClass);

    PropertySetter result = reference == null ? null : reference.get();
    if (result == null) {
        result = new PropertySetter(beanClass);
        CACHE.put(beanClass, new SoftReference<PropertySetter>(result));
    }
    return result;
}

From source file:com.sworddance.util.WeakProxy.java

public static <T> T getReferent(Reference<T> reference) {
    if (reference == null) {
        return null;
    } else {/*  w  w  w  . j a va 2s . co m*/
        return reference.get();
    }
}

From source file:net.ontopia.topicmaps.query.utils.QueryUtils.java

/**
 * PUBLIC: Returns the default query processor for the given topic
 * map and base address. Will always return the same processor for
 * the same (query language, topic map, base address) combination.
 *
 * @since 2.0/*  w w  w .j av  a 2 s .  c o  m*/
 */
public static QueryProcessorIF getQueryProcessor(String queryLanguage, TopicMapIF topicmap, LocatorIF base) {
    // Get {LocatorIF : QueryProcessorIF} entry
    Map<LocatorIF, Map<String, Reference<QueryProcessorIF>>> qps = qpcache.get(topicmap);
    if (qps == null) {
        qps = new HashMap<LocatorIF, Map<String, Reference<QueryProcessorIF>>>();
        qpcache.put(topicmap, qps);
    }

    Map<String, Reference<QueryProcessorIF>> refmap = qps.get(base);
    if (refmap == null) {
        refmap = new HashMap<String, Reference<QueryProcessorIF>>();
        qps.put(base, refmap);
    }

    // Get QueryProcessorIF
    Reference<QueryProcessorIF> ref = refmap.get(queryLanguage);
    if (ref != null) {
        if (ref.get() != null) {
            return ref.get();
        }
    }

    QueryProcessorIF qp = createQueryProcessor(queryLanguage, topicmap, base);
    refmap.put(queryLanguage, new SoftReference<QueryProcessorIF>(qp));
    return qp;
}