1. A dynamic proxy

JDK dynamic Proxy: implement InvacationHandler, then use proxy.newproxyinstance (Classloader, Class<? >, Handler) call. Cglib dynamic proxy: Implements MethodInterceptor, CglibProxyFactory enchanner. setCallBack(custom Interceptor). CglibProxyFactory.getProxy(); Cglib can delegate classes, which are implemented with ASM, whereas the JDK can only delegate interfaces. The principle is that the JVM generates bytecode dynamically.

I/O BIO: synchronizes blocking I/O, copy kernel-mode to user-mode blocking NIO: synchronizes non-blocking I/O, listens on multiple channels through selector, non-blocking, and returns when the process is complete. The thread first issues a SELECT call, asking the kernel if the data is ready, and then the kernel issues read when it is ready. AIO: notification callback.

Set 3.

ArrayList: Object[] array Vector: Object[] array LinkedList: bidirectional list 1.6 and before are circular linked lists. HashSet: unordered, unique HashMap LinkedHashSet: LinkedHashMap implementation. TreeSet: ordered, unique red-black tree (self-balanced sorted binary tree) HashMap: array + list + red-black tree (list threshold 8, expansion factor 0.75), 1.7 and below array + list LinkedHashMap: HashMap based on Node added a bidirectional list. TreeMap: a self-balanced sort binary tree

3.1. list constructor: if size is not specified, jdk7 constructs an array of 10 by default, and JDk8 constructs an empty array by default. Add: Get the minimum capacity (size+1, 10). If size+1 exceeds the maximum size of the current container, grow() to 1.5 times. Up to integer.max_value

3.2. HashMap 3.2.1. Why is the length a power of 2? The & operation to the power of 2 = % mod operation. Binary operation & can improve the efficiency of operation. 3.2.2. Dead-loop problem? When two threads in jdk7 are expanded at the same time, the order of the new list and the old list is inverted, and there will be an infinite loop of a.next. B, b.next. A, get(). Jdk8 new linked list and old linked list order is the same, the first plug is changed to tail, solve the problem. 3.2.3. EntrySet performs better than KeySet traversal? Because keySet calls map.get(key) again; put:

3.3. ConcurrentHashMap jdk7 uses Segment locking, jdk8 uses Node array + linked list + red and black. Locks are unfair. Tree array structure to achieve concurrency control using sychronized and CAS operations. (jdk1.6 has a lot of optimizations for sychronized locks). Initialization: Done with spin and CAS, sizectl-1 – indicates initialization; -n Indicates that N-1 threads are expanding. >0 If the table is not initialized, it indicates the initialized size of the table. If the table is initialized, it indicates the capacity of the table. Put: 1. Calculate hashcode based on key. 2. Determine whether to initialize the vm. 3. The Node is located for the current key. If the Node is empty, data can be written to the current position. 4. If the current location of hashCode == MOVED == -1, the capacity needs to be expanded. 5. If none is met, the sychronized lock is written. 6. If the number >TREEIFY_THERSHOLD, convert to red-black tree. Get: 1. Compute the position based on the hash. 2. Search for the specified location. If the first node is found, return value. 3. If the head node hash is less than 0, expansion is being performed or the red-black tree is being expanded. 4. If it is a linked list, search for it. 1. CounterCells == null to perform the CAS operation on baseCount; CountCells []; 3. CountCells failed to insert baseCount; The whole process is spin cycle. 2. Chain >8 and Map<64 3. PutAll does not fit behind elements for a closer look. The first thread, sizecl-2, is found to be expanding and assisting in the expansion. Each thread is responsible for one segment. During capacity expansion, the FWD identification class is used to point to a new table.

  1. concurrent

4.1. Threads and Processes A process is the basic unit of running programs in a system. Threads are a smaller unit than processes. Unlike processes, multiple threads share a process’s heap and method area resources, private program counters, virtual machine stacks, and local method stacks. Threads in the same process may interact. 4.2. Why are program counters, virtual machine stacks, and local method stacks private? Program counter is mainly used to record the location of the current thread execution, in order to restore the thread switchover to the correct location to continue execution; Each Java method in the virtual machine stack creates a stack frame that contains information about local variables, operand stacks, constant pool references, and so on. From the method call to the end of execution, is actually the corresponding stack frame from the stack to the stack process. Local variables are private to ensure that they are not accessed by other threads. 4.3. Thread life Cycle?

4.4. What is context switching? The current task saves its state before switching to another task after executing the CPU time slice, so that it can load the state of the task when switching to another task again. The process from saving to reloading a task is a context switch. 4.5. Deadlock A and B hold each other’s resources, deadlocking. Avoid deadlocks, obtain locks sequentially, request locks once, if the lock is not released. 4.6. sleep(), wait() wait releases lock, notitfy is invoked, sleep does not release lock. 4.7.run (), start() Start () will change the thread to ready state, the system automatically adjusts run(), 4.8. synchronized static and sychronized(class) lock the class; sychronized(class) lock the object instance. Monitorenter and Monitorexit can be seen in static bytecode execution sychronized. Monitorenter contests possession of a Monitor object and gets it when count =0. Monitorexit counts -1 and releases it when count =0. Method ACC_SYNCHRONIZED flag. Sychronized writes the Markword CAS in the object header. No lock-object hashcode, biased lock-thread ID, lightweight lock-stack lock record pointer, other thread spin, heavyweight lock-mutex pointer, other thread block. Volatile solves this problem. Visibility – Memory barrier, orderability – disallows instruction reordering.

Threadlocal A Thread contains a ThreadLocalMap, which stores objects whose Threadlocal is a Key and whose Object is a value. Key is a weak reference. Threadlocal hashes are stored using the linear exploration hash method. When stored, the threadLocal hash will find the values with null keys before and after, preventing memory leakage. InheritableThreadLocal inherits the InheritableThreadLocal class when the InheritableThreadLocal subclass is initialized. Thread pool core parameters Core thread, maximum thread, expiration time, expiration time unit, Queue, threadFactory, reject policy. Rejection strategy: direct discarding, throwing exception discarding, using their own threads to execute tasks, discarding the earliest tasks.

AtomicInteger Volatile + System CAS

4.13. AQS idea: When the shared resource is idle, the resource of the current requesting thread is set as a valid thread and the resource is locked. When the shared thread is locked, it needs a mechanism to allocate the lock when the thread is blocking, waiting and waking up. This mechanism is realized by the CLH queue of AQS (virtual bidirectional queue), and the thread that cannot obtain the lock is added to the queue.

4.14. Semaphore Semaphore – Allow multiple threads to access Semaphore.acquire (1) at the same time; semaphore.release(1); State > 0, can enter, state-1, otherwise hang in AQS etc aroused. CountDownLatch – Uncompleted block state == 0, invokes the main latch

4.16. CyclicBarrier with reset can be used multiple times

4.17. Read/write lock state 16 bits higher: records the number of threads that enter the read lock + the number of reentry times. State lower than 16 bits: records the reentrancy count of write locks.

4.18. ReentrantLock state =0, tryAcquire && addNode && checkInterrupt