Example usage for java.lang.ref WeakReference WeakReference

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

Introduction

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

Prototype

public WeakReference(T referent) 

Source Link

Document

Creates a new weak reference that refers to the given object.

Usage

From source file:com.android.gallery3d.app.PhotoPage.java

private void showEmptyAlbumToast(int toastLength) {
    Toast toast;//from ww  w  . j a  v  a2  s.c  o m
    if (mEmptyAlbumToast != null) {
        toast = mEmptyAlbumToast.get();
        if (toast != null) {
            toast.show();
            return;
        }
    }
    toast = Toast.makeText(mActivity, R.string.empty_album, SHOW_TOAST_DURATION);
    mEmptyAlbumToast = new WeakReference<Toast>(toast);
    toast.show();
}

From source file:com.linkedin.databus.core.DbusEventBuffer.java

protected void trackIterator(BaseEventIterator eventIterator) {
    synchronized (_busyIteratorPool) {
        _busyIteratorPool.add(new WeakReference<BaseEventIterator>(eventIterator));
    }//from w  w w  .j a v  a 2s .co  m
}

From source file:com.cohort.util.String2.java

/** 
 * This is like String.intern(), but uses a WeakHashMap so the canonical strings 
 * can be garbage collected.//from w  w  w  .  j  av  a2 s . c  om
 * <br>This is thread safe.
 * <br>It is fast: ~0.002ms per call.
 * <br>See TestUtil.testString2canonical().
 *
 * <p>Using this increases memory use by ~6 bytes per canonical string
 * (4 for pointer * ~.5 hashMap load factor).
 * <br>So it only saves memory if many strings would otherwise be duplicated.
 * <br>But if lots of strings are originally duplicates, it saves *lots* of memory.
 *
 * @param s  the string   (may be null)  (may be from s2.substring(start, stop))
 * @return a canonical string with the same characters as s.
 */
public static String canonical(String s) {
    if (s == null)
        return null;
    if (s.length() == 0)
        return "";
    //generally, it slows things down to see if same as last canonical String.
    char ch0 = s.charAt(0);
    Map tCanonicalMap = canonicalMap[ch0 < 'A' ? 0 : ch0 < 'N' ? 1 : //divide uppercase into 2 parts
            ch0 < 'a' ? 2 : ch0 < 'j' ? 3 : //divide lowercase into 3 parts
                    ch0 < 'r' ? 4 : 5];

    //faster and logically better to synchronized(canonicalMap) once 
    //  (and use a few times in consistent state)
    //than to synchronize canonicalMap and lock/unlock twice
    synchronized (tCanonicalMap) {
        WeakReference wr = (WeakReference) tCanonicalMap.get(s);
        //wr won't be garbage collected, but reference might (making wr.get() return null)
        String canonical = wr == null ? null : (String) (wr.get());
        if (canonical == null) {
            //For proof that new String(s.substring(,)) is just storing relevant chars,
            //not a reference to the parent string, see TestUtil.testString2canonical2()
            canonical = new String(s); //in case s is from s2.substring, copy to be just the characters
            tCanonicalMap.put(canonical, new WeakReference(canonical));
            //log("new canonical string: " + canonical);
        }
        return canonical;
    }
}