# 锁&&并发

面试官:

  1. 写一段死锁代码
  2. CAS底层是怎么实现的?
  3. Volatile 怎么实现的可见性?
  4. Synchronized原理?高版本Synchronized是怎么优化的?Synchronized一定比Lock性能差吗?
  5. AQS原理
  6. ConcurrentHashMap底层结构是什么?Put过程?各个版本之间有哪些变化?

并发包

锁:

死锁代码:

    public static void main(String[] args) {
        final Object a = new Object();
        final Object b = new Object();
        Thread threadA = new Thread(new Runnable() {
            public void run() {
                synchronized (a) {
                    try {
                        Thread.sleep(1000);
                        synchronized (b) {
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        });
        Thread threadB = new Thread(new Runnable() {
            public void run() {
                synchronized (b) {
                    try {
                        Thread.sleep(1000);
                        synchronized (a) {
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                    }
                }
            }
        });
        
        threadA.start();
        threadB.start();
    }

并发编程: