weakreference实现原理分析
来源:互联网 发布:企业网络初步设计方案 编辑:程序博客网 时间:2024/05/29 10:41
本文作者:RockyBalboa
原文地址:http://rockybalboa.blog.51cto.com/1010693/813161
前言
实例分析
- private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> {
- private V value;
- private final int hash;
- private Entry<K,V> next;
- Entry<K,V> e = tab[i];
- tab[i] = new Entry<K,V>(k, value, queue, h, e);
- if (++size >= threshold)
- resize(tab.length * 2);
- return null;
- }
- Reference(T referent, ReferenceQueue<? super T> queue) {
- this.referent = referent;
- this.queue = (queue == null) ? ReferenceQueue.NULL : queue;
- }
- /* Object used to synchronize with the garbage collector. The collector
- * must acquire this lock at the beginning of each collection cycle. It is
- * therefore critical that any code holding this lock complete as quickly
- * as possible, allocate no new objects, and avoid calling user code.
- */
- static private class Lock { };
- private static Lock lock = new Lock();
- void instanceRefKlass::acquire_pending_list_lock(BasicLock *pending_list_basic_lock) {
- // we may enter this with pending exception set
- PRESERVE_EXCEPTION_MARK; // exceptions are never thrown, needed for TRAPS argument
- Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
- ObjectSynchronizer::fast_enter(h_lock, pending_list_basic_lock, false, THREAD);
- assert(ObjectSynchronizer::current_thread_holds_lock(
- JavaThread::current(), h_lock),
- "Locking should have succeeded");
- if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
- }
- void instanceRefKlass::release_and_notify_pending_list_lock(
- BasicLock *pending_list_basic_lock) {
- // we may enter this with pending exception set
- PRESERVE_EXCEPTION_MARK; // exceptions are never thrown, needed for TRAPS argument
- //
- Handle h_lock(THREAD, java_lang_ref_Reference::pending_list_lock());
- assert(ObjectSynchronizer::current_thread_holds_lock(
- JavaThread::current(), h_lock),
- "Lock should be held");
- // Notify waiters on pending lists lock if there is any reference.
- if (java_lang_ref_Reference::pending_list() != NULL) {
- ObjectSynchronizer::notifyall(h_lock, THREAD);
- }
- ObjectSynchronizer::fast_exit(h_lock(), pending_list_basic_lock, THREAD);
- if (HAS_PENDING_EXCEPTION) CLEAR_PENDING_EXCEPTION;
- }
- /* List of References waiting to be enqueued. The collector adds
- * References to this list, while the Reference-handler thread removes
- * them. This list is protected by the above lock object.
- */
- private static Reference pending = null;
- public void run() {
- for (;;) {
- Reference r;
- synchronized (lock) {
- if (pending != null) {
- r = pending;
- Reference rn = r.next;
- pending = (rn == r) ? null : rn;
- r.next = r;
- } else {
- try {
- lock.wait();
- } catch (InterruptedException x) { }
- continue;
- }
- }
- // Fast path for cleaners
- if (r instanceof Cleaner) {
- ((Cleaner)r).clean();
- continue;
- }
- ReferenceQueue q = r.queue;
- if (q != ReferenceQueue.NULL) q.enqueue(r);
- }
- }
- }
- private void expungeStaleEntries() {
- Entry<K,V> e;
- while ( (e = (Entry<K,V>) queue.poll()) != null) {
- int h = e.hash;
- int i = indexFor(h, table.length);
- Entry<K,V> prev = table[i];
- Entry<K,V> p = prev;
- while (p != null) {
- Entry<K,V> next = p.next;
- if (p == e) {
- if (prev == e)
- table[i] = next;
- else
- prev.next = next;
- e.next = null; // Help GC
- e.value = null; // " "
- size--;
- break;
- }
- prev = p;
- p = next;
- }
- }
- }
阅读全文
0 0
- weakreference实现原理分析
- weakreference实现原理分析
- weakreference实现原理分析
- ThreadLocal与WeakReference 源码分析
- 用WeakReference实现共享节点
- WeakReference
- WeakReference
- WeakReference
- WeakReference
- WeakReference
- OgreTerrain 实现原理分析
- OgreTerrain 实现原理分析
- PACKET_MMAP实现原理分析
- BufferedInputStream实现原理分析
- Smarty实现原理分析
- PACKET_MMAP实现原理分析
- HashMap实现原理分析
- HashMap实现原理分析
- datasnap——动态注册服务类 datasnap——动态注册服务类 在DataSnap的应用程序时,我们首先需要注册的服务器类,以提供访问客户端应用程序的服务器方法。通常的做法是使用DSSer
- Nginx配置教程
- 实训笔记第十二天
- 服务器架构的演变
- 厦门工程技术人员职称评审 总体梳理
- weakreference实现原理分析
- 观察者模式(Observer Pattern)——让你的对象知悉现状
- JSP EL表达式 学习 应用
- 思维导图---区块链技术和区块链应用
- Combination Sum
- 深入spring cloud(1)
- POJ1598-C语言
- CodeForces 566 A Matching Names(Trie 匹配LCP和最大)
- git学习笔记2——ProGit2