Example usage for java.lang.ref ReferenceQueue poll

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

Introduction

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

Prototype

public Reference<? extends T> poll() 

Source Link

Document

Polls this queue to see if a reference object is available.

Usage

From source file:WeakCanonicalSet.java

/**
 * Pass in a candidate canonical object and get a unique instance from this
 * set. The returned object will always be of the same type as that passed
 * in. If the object passed in does not equal any object currently in the
 * set, it will be added to the set, becoming canonical.
 *
 * @param obj candidate canonical object; null is also accepted
 *///  w  ww .jav  a  2s  .  co m
public synchronized <U extends T> U put(U obj) {
    // This implementation is based on the WeakIdentityMap.put method.

    if (obj == null) {
        return null;
    }

    Entry<T>[] tab = this.table;

    // Cleanup after cleared References.
    {
        ReferenceQueue queue = this.queue;
        Reference ref;
        while ((ref = queue.poll()) != null) {
            // Since buckets are single-linked, traverse entire list and
            // cleanup all cleared references in it.
            int index = (((Entry) ref).hash & 0x7fffffff) % tab.length;
            for (Entry<T> e = tab[index], prev = null; e != null; e = e.next) {
                if (e.get() == null) {
                    if (prev != null) {
                        prev.next = e.next;
                    } else {
                        tab[index] = e.next;
                    }
                    this.count--;
                } else {
                    prev = e;
                }
            }
        }
    }

    int hash = hashCode(obj);
    int index = (hash & 0x7fffffff) % tab.length;

    for (Entry<T> e = tab[index], prev = null; e != null; e = e.next) {
        T iobj = e.get();
        if (iobj == null) {
            // Clean up after a cleared Reference.
            if (prev != null) {
                prev.next = e.next;
            } else {
                tab[index] = e.next;
            }
            this.count--;
        } else if (e.hash == hash && obj.getClass() == iobj.getClass() && equals(obj, iobj)) {
            // Found canonical instance.
            return (U) iobj;
        } else {
            prev = e;
        }
    }

    if (this.count >= this.threshold) {
        // Rehash the table if the threshold is exceeded.
        rehash();
        tab = this.table;
        index = (hash & 0x7fffffff) % tab.length;
    }

    // Create a new entry.
    tab[index] = new Entry<T>(obj, this.queue, hash, tab[index]);
    this.count++;
    return obj;
}

From source file:WeakIdentityMap.java

private void cleanup() {
    // Cleanup after cleared References.
    Entry[] tab = this.table;
    ReferenceQueue queue = this.queue;
    Reference ref;//from w w  w  . j a  v  a2s  . com
    while ((ref = queue.poll()) != null) {
        // Since buckets are single-linked, traverse entire list and
        // cleanup all cleared references in it.
        int index = (((Entry) ref).hash & 0x7fffffff) % tab.length;
        for (Entry e = tab[index], prev = null; e != null; e = e.next) {
            if (e.get() == null) {
                this.modCount++;
                if (prev != null) {
                    prev.next = e.next;
                } else {
                    tab[index] = e.next;
                }
                this.count--;
            } else {
                prev = e;
            }
        }
    }
}