android面试录

java面试题整理

2020-01-13  本文已影响0人  白茫茫的大地

堆和栈

java软引用与弱引用区别

参考了一些资料

final变量用反射修改

字符串hash函数

    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

ThreadLocal

private void set(ThreadLocal<?> key, Object value) {

    // We don't use a fast path as with get() because it is at
    // least as common to use set() to create new entries as
    // it is to replace existing ones, in which case, a fast
    // path would fail more often than not.

    Entry[] tab = table;
    int len = tab.length;
    int i = key.threadLocalHashCode & (len-1);
    // 这里就是采用的线性探测法。一直遍历这个数组,然后找到值
    for (Entry e = tab[i]; e != null; e = tab[i = nextIndex(i, len)]) {
        ThreadLocal<?> k = e.get();
        // 找到值了,替换掉原来的值
        if (k == key) {
            e.value = value;
            return;
        }
        // 发现这个位置上没有值,就讲这个位置设置对应的值
        if (k == null) {
            replaceStaleEntry(key, value, i);
            return;
        }
    }
    // tab[i]==null,这里直接new
    tab[i] = new Entry(key, value);
    int sz = ++size;
    // cleanSomeSlots : 由于是弱引用,所以这个地方,他在进行一个填充值的时候,进行了一个额外的删除已经被GC的Key对应的Value
    if (!cleanSomeSlots(i, sz) && sz >= threshold)
        // 这里面会调用扩容的方法
        rehash();
}




编码格式

抽象类和接口的区别

volatile

用于保持内存可见性(随时见到的都是最新值)和防止指令重排序 参考

java内存模型
volatile保持可见性
防止指令重排
  class Singleton {
    private static Singleton instance;
    private Singleton(){}
    public static Singleton getInstance() {
        if ( instance == null ) { //当instance不为null时,仍可能指向一个“被部分初始化的对象”
            synchronized (Singleton.class) {
                if ( instance == null ) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

它可以”抽象“为下面几条JVM指令:

memory = allocate();    //1:分配对象的内存空间
initInstance(memory);   //2:初始化对象
instance = memory;      //3:设置instance指向刚分配的内存地址

JVM可以以“优化”为目的对它们进行重排序,经过重排序后如下:

memory = allocate();    //1:分配对象的内存空间
instance = memory;      //3:设置instance指向刚分配的内存地址(此时对象还未初始化)
ctorInstance(memory);   //2:初始化对象

引用instance指向了一个"被部分初始化的对象"。此时,如果另一个线程调用getInstance方法,由于instance已经指向了一块内存空间,从而if条件判为false,方法返回instance引用,用户得到了没有完成初始化的“半个”单例。

解决这个该问题,只需要将instance声明为volatile变量:

private static volatile Singleton instance;

Thread类的sleep() yield() 和 wait()的区别?

多线程如何保证线程安全

GC

参考

找到被回收的对象
回收算法
分代收集
其他

List,Set,Map的区别

HashMap

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        // 当前位置为null,添加数据
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            // 两个值的hash值一样,key一样或者equals
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode) // 红黑树
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else { // 普通链表
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) { // 最后一个数值为null
                        p.next = newNode(hash, key, value, null); // 插入到尾部
                        // 大于某个值的时候,将链表转换为红黑树
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    // 判断是否有重复的数据
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            // 如果有相同的key,进行替换
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                
                afterNodeAccess(e);
                return oldValue;
            }
        }
        // fail-fast机制
        ++modCount;
        // 判断是否已经达到阈值,进行扩容处理
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

ConcurrentHashMap

HashSet

ArrayList & LinkedList

https

Java动态代理

classloader

JVM内存模型,内存区域

死锁

上一篇 下一篇

猜你喜欢

热点阅读