`

Java reference

阅读更多

Reference思考

原写于 2010-12-18

 

一. Java数据类型

数据类型就是对内存位置的抽象表达(很多编程语言都依赖于特定的计算机类型和对数据类型属性的具体编译实现,比如word和integer数据类型的大小等;Java通过JVM保证数据所占存储空间的大小不会随硬件的改变发生变化)。

 

1.Primitive data type :A primitive type is predefined by the language and is named by a reserved keyword. Primitive values do not share state with other primitive values. The eight primitive data types supported by the Java programming language。

原生类型是语言自身预先定义的,原生值不会与其他原生值共享状态。有8中原生类型被Java语言支持,如下:byte(8位)、short(16位)、int(32位)、long(64位)、float(浮点数,32位)、double(浮点数,64位)、boolean(1位,只有true或false可取)、char(16-bit Unicode character)

 

2.复杂数据类型

运行期动态创建,一切皆为对象。

- 接口(定义行为)

- 类(String objects are immutable, which means that once created, their values cannot be changed.)

- 数组

- 引用(软、弱、虚引用)

 

二. Reference分类

1. strong Reference :正常情况下的引用都属于strong引用,如:String str = new String();str是strong reference。其它引用必须是显式的声明,如:SoftReference softRef = new SoftReferencne(str),softRef是弱引用。

2. SoftReference :软引用。在内存溢出前,垃圾收集器将回收其引用的对象。

3. WeakReference :弱引用。任何垃圾回收,扫描到弱引用时,其引用的对象都会被回收。

4. PhantomReference :不理解。

写道
补充:ReferenceQueue
Reference queues, to which registered reference objects are appended by the garbage collector after the appropriate reachability changes are detected.
当某个引用对象的可达性被垃圾搜集器改变或清除后,会被放入对应的 ReferenceQueue 中(如果注册引用队列)。
String str = new String("hello");
ReferenceQueue<String> refQuence = new ReferenceQueue<String>();
SoftReference<String> softRef = new SoftReference<String>(str, refQuence);
str = null; // 垃圾回收
如上例:当“hello”对象被收回后,则softRef会被加入到ReferenceQueue。

 

 

三.WeakHashMap简单分析

 

简化WeakHashMap结构:

class WeakHashMap<K, V> {
	private final ReferenceQueue<K> queue = new ReferenceQueue<K>();
	private Entry[] table;
	private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> {}
}

分析:WeakHashMap的put方法(Map对数据的保存也是通过数组实现)

public V put(K key, V value) {
        K k = (K) maskNull(key);
        int h = HashMap.hash(k.hashCode());
        Entry[] tab = getTable(); //获取table(关键:调用expungeStaleEntries方法,清洗table数组中,value所指的实际对象已被GC)
        int i = indexFor(h, tab.length); //根据key.hash计算在数组的位置

		//从此位置开始,遍历单向链表,判断此位置是否已经存在相同元素
		//1.有,更新为value
//2.无,创建新的对象,置原头部元素为自己的next,让自身为单向链表头部
        for (Entry<K,V> e = tab[i]; e != null; e = e.next) {
			//如果hash相等,且equals相等
            if (h == e.hash && eq(k, e.get())) {
                V oldValue = e.value;
                if (value != oldValue)
                    e.value = value;
                return oldValue;
            }
        }
        modCount++;
	    Entry<K,V> e = tab[i];
		//queue是ReferenceQueue,h自身key计算得到的hash值,e指向单链表下一个节点next
        tab[i] = new Entry<K,V>(k, value, queue, h, e);
        if (++size >= threshold) //判断数组容量是否需要扩充,策略:2倍
            resize(tab.length * 2);
        return null;
    }

/*
* 清洗已被gc对象的引用,释放数组的容量
*/
private void expungeStaleEntries() {
		Entry<K,V> e;
        while ( (e = (Entry<K,V>) queue.poll()) != null) { //从队列获取第一个加入ReferenceQueue的引用,循环
            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) { //若相等,说明引用的对象已被gc
                    if (prev == e)
                        table[i] = next;
                    else
                        prev.next = next;
				    //因为其引用对象被垃圾收集器改变可达性时,也会被放入queue,所以强制设置其next、value为null,帮助gc收集其引用对象(不这样的理解正确不?请大家指点)
                    e.next = null;  // Help GC
                    e.value = null; //  "   "
                    size--;
                    break;
                }
                prev = p;
                p = next;
            }
        }
    }

 

 

 四.总结

 引用也是属于Java的比较特殊的数据类型,对于软、弱引用的使用场景大多是“缓存”。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics