Example usage for java.lang.ref ReferenceQueue remove

List of usage examples for java.lang.ref ReferenceQueue remove

Introduction

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

Prototype

public Reference<? extends T> remove() throws InterruptedException 

Source Link

Document

Removes the next reference object in this queue, blocking until one becomes available.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ReferenceQueue rq = new ReferenceQueue();
    WeakReference<String> wr = new WeakReference<String>("string", rq);

    while (true) {
        Reference r = rq.remove();
        if (r == wr) {
            System.out.println("no longer referenced");
        }//  w  ww.j  av a 2 s.  c o  m
    }
}

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*/
        }
    }
}