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:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map weakMap = new WeakHashMap();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference(valueObject);

    weakMap.put(keyObject, weakValue);/*from   www  .  j  a  v a  2s  . c om*/

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object keyObject = "";
    Object valueObject = "";
    Map<Object, Object> weakMap = new WeakHashMap<Object, Object>();

    weakMap.put(keyObject, valueObject);
    WeakReference weakValue = new WeakReference<Object>(valueObject);

    weakMap.put(keyObject, weakValue);/*from w ww.j a  v a2s.c  o  m*/

    Iterator it = weakMap.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();
        weakValue = (WeakReference) weakMap.get(key);
        if (weakValue == null) {
            System.out.println("Value has been garbage-collected");
        } else {
            System.out.println("Get value");
            valueObject = weakValue.get();
        }
    }
}

From source file:Main.java

public static void forceGCandWait() {
    Object obj = new Object();
    WeakReference ref = new WeakReference<>(obj);
    obj = null;//from w ww.ja v a 2s.  co m

    System.gc();
    System.runFinalization();
    /** wait for garbage collector finished*/
    while (ref.get() != null)
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
        } //System.gc();
}

From source file:Main.java

public static Intent getIntent(Context context, Class<?> actyClass) {
    Context loContext = new WeakReference<Context>(context).get();
    Intent intent = new Intent(loContext, actyClass);
    return intent;
}

From source file:Main.java

public static void startService(Context context, Class<?> actyClass) {
    Context loContext = new WeakReference<Context>(context).get();
    Intent intent = new Intent(loContext, actyClass);
    loContext.startService(intent);//  w  w  w .j  ava 2  s . c  om
}

From source file:Main.java

public static void observeObject(Object obj) {
    list.add(new WeakReference<Object>(obj));
}

From source file:Main.java

private static void setContext(Context context) {
    contextWR = new WeakReference<Context>(context);
}

From source file:Main.java

public static void addActivity(Activity a) {
    activitys.add(new WeakReference(a));
}

From source file:Main.java

public static void executeOnGlobalLayout(View view, final Runnable runnable) {
    final WeakReference<View> viewReference = new WeakReference<>(view);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override/*from  w  ww .j a  v  a 2  s .  c om*/
        public void onGlobalLayout() {
            removeOnGlobalLayoutListener(viewReference.get(), this);
            runnable.run();
        }
    });

}

From source file:Main.java

public synchronized static Thread CreateThread(Runnable run) {
    Thread thread = new Thread(run);
    threadlist.add(new WeakReference<>(thread));
    return thread;
}