public WeakPool()
{ this.loadFactor = DEFAULT_LOAD_FACTOR;
threshold = DEFAULT_INITIAL_CAPACITY;
table = new Entry[DEFAULT_INITIAL_CAPACITY];
}
/** *Checkforequalityofnon-nullreferencexandpossibly-nully.By *defaultusesObject.equals.
*/ privateboolean eq(Object x, Object y)
{ return x == y || x.equals(y);
}
/** *Returnindexforhashcodeh.
*/ privateint indexFor(int h, int length)
{ return h & length - 1;
}
/** *Expungestaleentriesfromthetable.
*/ privatevoid expungeStaleEntries()
{
Object r; while ((r = queue.poll()) != null)
{
Entry e = (Entry) r; int h = e.hash; int i = indexFor(h, table.length);
// System.out.println("EXPUNGING " + h);
Entry<V> prev = table[i];
Entry<V> p = prev; while (p != null)
{
Entry<V> next = p.next; if (p == e)
{ if (prev == e)
{
table[i] = next;
} else
{
prev.next = next;
}
e.next = null; // Help GC
size--; break;
}
prev = p;
p = next;
}
}
}
/** *Returnsthevaluestoredinthepoolthatequalstherequestedkey *or<tt>null</tt>ifthemapcontainsnomappingfor *thiskey(orthekeyisnull) * *@paramkeythekeywhoseequalsvalueistobereturned. *@returntheobjectthatisequalthespecifiedkey,or *<tt>null</tt>ifkeyisnullornoobjectinthepoolequalsthekey.
*/ public V get(V key)
{ if (key == null)
{ returnnull;
} int h = key.hashCode();
Entry<V>[] tab = getTable(); int index = indexFor(h, tab.length);
Entry<V> e = tab[index]; while (e != null)
{
V candidate = e.get(); if (e.hash == h && eq(key, candidate))
{ return candidate;
}
e = e.next;
} returnnull;
}
/** *ReturnstheentryassociatedwiththespecifiedkeyintheHashMap. *ReturnsnulliftheHashMapcontainsnomappingforthiskey.
*/
Entry getEntry(Object key)
{ int h = key.hashCode();
Entry[] tab = getTable(); int index = indexFor(h, tab.length);
Entry e = tab[index]; while (e != null && !(e.hash == h && eq(key, e.get())))
{
e = e.next;
} return e;
}
/** *Placestheobjectintothepool.Iftheobjectisnull,nothinghappens. *Ifanequalobjectalreadyexists,itisnotreplaced. * *@paramkeytheobjecttoputintothepool.keymaybenull. *@returntheobjectinthepoolthatisequaltothekey,orthenewlyplacedkeyifnosuchobjectexistedwhenputwascalled
*/ public V put(V key)
{ if (key == null)
{ returnnull;
} int h = key.hashCode();
Entry<V>[] tab = getTable(); int i = indexFor(h, tab.length);
for (Entry<V> e = tab[i]; e != null; e = e.next)
{
V candidate = e.get(); if (h == e.hash && eq(key, candidate))
{ return candidate;
}
}
tab[i] = new Entry<V>(key, queue, h, tab[i]);
if (++size >= threshold)
{
resize(tab.length * 2);
}
/** *Transferallentriesfromsrctodesttables
*/ privatevoid transfer(Entry[] src, Entry[] dest)
{ for (int j = 0; j < src.length; ++j)
{
Entry e = src[j];
src[j] = null; while (e != null)
{
Entry next = e.next;
Object key = e.get(); if (key == null)
{
e.next = null; // Help GC
size--;
} else
{ int i = indexFor(e.hash, dest.length);
e.next = dest[i];
dest[i] = e;
}
e = next;
}
}
}
/** *Removestheobjectinthepoolthatequalsthekey. * *@paramkey *@returnpreviousvalueassociatedwithspecifiedkey,or<tt>null</tt> *iftherewasnomappingforkeyorthekeyisnull.
*/ public V removeFromPool(V key)
{ if (key == null)
{ returnnull;
} int h = key.hashCode();
Entry<V>[] tab = getTable(); int i = indexFor(h, tab.length);
Entry<V> prev = tab[i];
Entry<V> e = prev;
while (e != null)
{
Entry<V> next = e.next;
V candidate = e.get(); if (h == e.hash && eq(key, candidate))
{
size--; if (prev == e)
{
tab[i] = next;
} else
{
prev.next = next;
} return candidate;
}
prev = e;
e = next;
}
returnnull;
}
/** *Removesallmappingsfromthismap.
*/ publicvoid clear()
{ // clear out ref queue. We don't need to expunge entries // since table is getting cleared. while (queue.poll() != null)
{ // nop
}
table = new Entry[DEFAULT_INITIAL_CAPACITY];
threshold = DEFAULT_INITIAL_CAPACITY;
size = 0;
// Allocation of array may have caused GC, which may have caused // additional entries to go stale. Removing these entries from the // reference queue will make them eligible for reclamation. while (queue.poll() != null)
{ // nop
}
}
test.ml1 = new MultiSynonymKey(new SingletonList(new String(test.arg1)));
test.ml2 = new MultiSynonymKey(new DoubletonList(new String(test.arg1), new String(test.arg2)));
test.ml3 = new MultiSynonymKey(new DoubletonList(new String(test.arg1), new String(test.arg2)));
wp.put(test.ml1);
wp.put(test.ml2);
test.setDaemon(true);
test.start();
int counter = 0; while (true) { synchronized (wp) {
MultiSynonymKey foo = test.ml3;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.