Dahua Group interviewed Java development

Dahua Group interviewed Java development

JVM memory model

src=http___www.pianshen.com_images_608_d62315385d33442709a2a85ea8c27d88.png&refer=http___www.pianshen

The JVM memory structure mainly has three major blocks: heap memory , method area , and stack . The heap memory is the largest part of the JVM, which is composed of the new generation and the old generation, and the new generation memory is divided into three parts, Eden space , From Survivor space , and To Survivor space . By default, the young generation is in a ratio of 8:1:1 to allocate;

Method area storageclass informationconstantstatic variableWaiting for data is an area shared by threads . To distinguish it from the Java heap, the method area also has an alias Non-Heap (non-heap); the stack is divided into a java virtual machine stack and a local method stack, which are mainly used for method execution.

method area and heapis allthread sharingmemory area ; and **Java stack, native method stack (Native Method Stack) and programmer counteryes run yesthread privateThe memory area**.

99% of JVM tuning is done in the heap , and there will be no garbage in the Java stack, native method stack, and program counter .

method area(Method Area), which is a memory area shared by threads , which is used to store data that has been loaded by the virtual machineClass information, constants, static variables, code compiled by the just-in-time compilerand other data. Java 1.8 was called PermGen before , and it was renamed MetaSpace in 1.8 . It is expressed as MetaSpace later, and the constant pool is located in the method area .

Java heap(Java Heap) Like the method area, it is a memory area shared by threads , and the largest piece of memory managed by the Java virtual machine . Created when the virtual machine starts , the sole purpose of this memory area is**store object instance**, almost all object instances allocate memory here , not all.

program countersmall memory space,thread private. The job of the bytecode interpreter is to change the value of the counter to select the next bytecode instruction that needs to be executed. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all rely on the counter to complete.

Java virtual machine stack(Java Virtual Machine Stacks) also **thread privateYes, the lifecycle is the same as a thread . The virtual machine stack describes the memory model of Java method execution: when each method is executed, a stack frame (Stack Frame) is created at the same time forStore local variable table, operation stack, dynamic link, method exit, etc.information**.

native method stack(Native Method Stacks) is very similar to the role played by the virtual machine stack, and it is also**thread privateYes, the difference is that the virtual machine stack serves for the virtual machine to execute Java methods (that is, bytecodes), while the local method stack serves for the Native methods used by the virtual machine**. The language, usage and data structure used for the methods in the local method stack in the virtual machine specificationnot mandatory, so concrete virtual machines are free to implement it. Even some virtual machines (such as the Sun HotSpot virtual machine) directly combine the local method stack and the virtual machine stack into one.

GC Algorithm and Garbage Collector

In the JVM, the program counter, the virtual machine stack, and the local method stack are all born and destroyed with the thread . The stack frame is pushed and popped as the method enters and exits, realizing automatic memory cleanup, so , memory garbage collection is mainly concentrated in the java heap and method area . During the running of the program, the allocation and use of this part of memory are dynamic .

Garbage collection process in the heap (GC main area)

  1. If the object finds that there is no reference chain connected to GC Roots after the reachability analysis, it will be marked for the first time;
  2. Determine whether it is necessary to execute the finalize () method of the object (it is not overridden, it has been called, and it is not necessary to execute it), and put it into the F-Queue queue;
  3. Put it into the F-Queue for the second marking;
  4. Removal queues that are rescued, and those that are marked twice are recycled;

garbage collection algorithm

  1. Mark-sweep algorithm :
  2. Copy algorithm : copy the living object to another block, and then empty the former
  3. Mark-sorting algorithm : Let all surviving objects move to one end, and then directly clean up the memory outside the end boundary
  4. Generational collection algorithm : Divide the Java heap into the new generation and the old generation, so that the most appropriate collection algorithm can be adopted according to the characteristics of each age;
  5. HotSpot algorithm : In the implementation of HotSpot, a set of data structures called OopMap are used to store object references;

garbage collector

New Generation Collector : Serial Collector, ParNew Collector, Parallel Scavenge Collector ------ Copy Algorithm

Old age collectors : Serial Old collector (mark finishing), Parallel Old collector (mark finishing), CMS collector (mark clearing)

G1 collector (important) : the collection range includes the new generation and the old generation; combined with multiple garbage collection algorithms, space integration, no fragmentation; high throughput;

Java multithreading

How Java threads are created

Thread creation and startup : The JVM of the Java language allows programs to run multiple threads, which is reflected by the java.lang.Thread class.

Features of the Thread class

  1. Each thread completes the operation through the run() method of a specific Thread object, and the main body of the run() method is often called the thread body;
  2. Start the thread through the start() method of the Thread object instead of calling run() directly.

Method 1: Implement the Runnable interface

  1. Create a class that implements the Runnable interface
  2. The implementation class implements the abstract method in Runnable: run()
  3. Create an object of the implementation class
  4. Pass this object as a parameter to the constructor of the Thread class to create an object of the Thread class
  5. Call start() through an object of the Thread class

Compare the two ways of creating threads :

In development : the way to implement the Runnable interface is preferred

Reason :

  1. The implementation method does not have the limitation of single inheritance of classes;
  2. The implementation method is more suitable for dealing with the situation where multiple threads have shared data;

联系:public class Thread implements Runnable。

The same point : Both methods need to rewrite run(), and declare the logic to be executed by the thread in run().

Method 2: Inherit from the Thread class

  1. Create a subclass that inherits from the Thread class and also implements the Runnable interface
  2. Rewrite the run() of the Thread class --> declare the operations performed by this thread in run()
  3. Create an object of a subclass of the Thread class
  4. Call the start()—start() method through this object. Function: ①Start the current thread ②Call the run() of the current thread

Method 3: Implement the Callable interface

  1. Compared with using Runnable, Callable is more powerful;
  2. Compared with the run() method, it can have a return value;
  3. Methods can throw exceptions;
  4. Support for generic return values;
  5. You need to use the FutureTask class, such as getting the return result;

Method 4: Use the thread pool

Idea: Create multiple threads in advance, put them into the thread pool, get them directly when using them, and put them back into the pool after use. It can avoid frequent creation and destruction and achieve reuse. Similar to public transportation in life.

The difference between wait and sleep

wait sleep
Synchronize The wait method can only be called in a synchronization context, otherwise an IllegalMonitorStateException will be thrown Does not need to be called in a synchronized method or synchronized block
Action object The wait method is defined in the Object class and acts on the object itself The sleep method is defined in java.lang.Thread and acts on the current thread
release lock resource yes no
wake up condition Other threads call the object's notify() or notifyAll() method Timeout or call interrupt() method body
method attribute wait is an instance method sleep is a static method

Mutual Exclusion and Synchronization of Threads

When multiple threads need to access the same resource, only one thread is allowed to operate the shared resource within a period of time, and other threads can read the resource after the operation is completed, which is called mutual exclusion of threads. We need to use synchronized to lock the shared area to ensure the security of shared resources.

Method 1: Synchronize code blocks

synchronized(sync monitor) {

//Code that needs to be synchronized

}

Method 2: Synchronous method.

If the code for operating shared data is completely declared in a method, we might as well declare this method to be synchronous.

Use the synchronization method to solve the thread safety problem of implementing the Runnable interface, use the wait and notify methods, synchronized

notify() or notifyAll() method: the method wakes up the specified thread

Method 3: Use conditional lock ReentrantLock

  1. Starting from JDK 5.0, Java provides a more powerful thread synchronization mechanism - synchronization is achieved by explicitly defining synchronization lock objects. Synchronization locks are implemented using Lock objects.
  2. The java.util.concurrent.locks.Lock interface is a tool for controlling access to shared resources by multiple threads. The lock provides exclusive access to shared resources. Only one thread can lock the Lock object at a time, and the thread should obtain the Lock object before it starts to access the shared resource.
  3. The ReentrantLock class implements Lock, which has the same concurrency and memory semantics as synchronized. In implementing thread-safe control, ReentrantLock is more commonly used, which can explicitly lock and release locks.

Interview question: similarities and differences between synchronized and Lock

The same : both can solve the thread safety problem

Different : the synchronized mechanism automatically releases the synchronization monitor after executing the corresponding synchronization code

Lock needs to manually start synchronization (lock()), and end synchronization also needs to be manually implemented (unlock())

**Priority order of use: **Lock —> >synchronous code block (has entered the method body and allocated corresponding resources)---->>synchronous method (outside the method body)

reentrant lock

Reentrant lock refers to the unit of thread. When a thread acquires the object lock, the thread can acquire the lock on the object again, while other threads cannot.

Both synchronized and ReentrantLock are reentrant locks.

One of the meanings of reentrant locks is to prevent deadlocks.

The implementation principle is achieved by associating a request counter with a thread that holds it for each lock. When the count is 0, the lock is considered unoccupied; when a thread requests an unoccupied lock, the JVM will record the occupant of the lock and set the request counter to 1.

If the same thread requests this lock again, the counter will be incremented;

Each time the occupying thread exits the synchronized block, the counter value will be decremented. Until the counter reaches 0, the lock is released.

Regarding the re-entry of the locks of the parent class and the sub-class: the sub-class overrides the synchonized method of the parent class, and then calls the method in the parent class. If there is no reentrant lock at this time, then this code will generate a deadlock ( It's easy to understand).

AQS

Principle of AQS

AQS: AbstractQuenedSynchronizer abstract queue synchronizer. It is a lock mechanism other than the synchronized keyword that comes with java.
The full name of AQS (AbstractQueuedSynchronizer), this class is in the java.util.concurrent.locks package

AQS encapsulates each thread requesting shared resources into a node (Node) of a CLH lock queue to realize lock allocation.

AQS is based on the CLH queue, and the shared variable state is modified with volatile. The thread changes the state symbol through CAS. If it succeeds, it will acquire the lock successfully. If it fails, it will enter the waiting queue and wait to be woken up.

**Note: AQS is a spin lock: **When waiting to wake up, it often uses the spin (while(!cas())) method to keep trying to acquire the lock until it is successfully acquired by other threads;

The locks that implement AQS include: spin locks, mutex locks, read locks, write locks, conditional yields, semaphores, and fences are all derivatives of AQS

Understanding of the volatile keyword

Shared variables modified by volatile have the following two characteristics:

1. Guarantee the memory visibility of different threads operating on the variable;

2. Disable instruction reordering;

**1. Atomicity: **In Java, the reading and assignment operations of basic data types are atomic operations. The so-called atomic operations mean that these operations are uninterruptible and must be completed, or It is not implemented. Although the volatile keyword guarantees memory visibility and ordering, it does not guarantee atomicity.

**2. Visibility: **Java uses volatile to provide visibility. When a variable is modified by volatile, the modification to it will be immediately refreshed to the main memory, and when other threads need to read the variable, they will go to the memory to read the new value. Ordinary variables do not guarantee this.

In fact, visibility can also be guaranteed through synchronized and Lock. Before the thread releases the lock, it will flush the value of the shared variable back to the main memory, but the overhead of synchronized and Lock is greater.

3. Ordering

JMM allows compilers and processors to reorder instructions, but specifies as-if-serial semantics, that is, no matter how reordered, the execution result of the program cannot be changed.

Transaction isolation level (key)

Classification:

  1. Read uncommitted (Read uncommitted) : Dirty reads/non-repeatable reads/phantom reads are possible
  2. Read committed (RC for short) : Dirty reads are impossible, non-repeatable reads/phantom reads are possible
  3. Repeatable read (Repeatable read referred to as RR) : non-repeatable read/dirty read impossible, phantom read possible
  4. Serializable : Dirty reads, non-repeatable reads, and phantom reads are impossible. When the transaction isolation level in MySQL is serializable, the table will be locked, so there will be no phantom reads. This isolation level The concurrency is extremely low and is rarely used in development .

Dirty Reads : Transaction A reads the data that has been modified by transaction B but has not yet been committed, and also operates on this data basis. At this time, if transaction B is rolled back, the data read by A is invalid and does not meet the consistency requirements;

Non-Repeatable Reads : A transaction reads the previously read data at a certain time after reading some data, but finds that the read data has changed, or some records have been deleted. deleted! This phenomenon is called non-repeatable read; transaction A reads the modified data submitted by transaction B, which does not meet the isolation .

Phantom Reads : A transaction re-reads previously retrieved data according to the same query conditions, but finds that other transactions have inserted new data that meets its query conditions. This phenomenon is called "phantom reads".

The default transaction isolation level of the InnoDB storage engine is Repeatable read (repeatable read) , and MVCC + Next-Key Lock can solve the phantom read problem .

image-20210917224407096

image-20210917224426279

MySQL engine, InnoDB vs. MyISAM

MyISAM Features

1) Does not support row locks (MyISAM only has table locks) , locks all tables that need to be read when reading, and adds exclusive locks to tables when writing;

2) Does not support transactions

3) Foreign keys are not supported

4) Safe recovery after a crash is not supported

5) While the table has a read query, it supports inserting new records into the table

6) Support the first 500 character index of BLOB and TEXT, support full-text index

7) Support delayed update index, which greatly improves write performance

8) For tables that will not be modified, it supports compressed tables, which greatly reduces the disk space occupied

InnoDB features :

1) Support row locks , use MVCC to support high concurrency, there may be deadlock

2) Support transactions

3) Support foreign keys

4) Support safe recovery after crash

5) Does not support full-text indexing

Main difference:

1) Whether to support transactions;

2) Whether to support row-level locks;

3) Can it be safely recovered after a crash;

4) The difference in count operation: Because MyISAM cache has table meta-data (number of rows, etc.), it does not need to consume much resources for a well-structured query when doing COUNT( ). For InnoDB, there is no such cache. But both need to execute count() query if there is a where condition.

Mysql's row lock and table lock (lock is a mechanism for a computer to coordinate multiple processes or pure threads to access a certain resource concurrently) table-level lock: each operation locks the entire table. Low overhead, fast locking; no deadlocks; large locking granularity, the highest probability of lock conflicts, and the lowest concurrency; row-level lock: each operation locks a row of data. The overhead is high, and locking is slow; there will be deadlocks; the locking granularity is the smallest, the probability of lock conflicts is the lowest, and the concurrency is also the highest;

The use of indexes in MySQL

Indexes are used to quickly find rows with a specific value in a certain column. Without indexes, MySQL must read the entire table from the first record until it finds the relevant rows. The larger the table, the more expensive it is to query data The more time, if there is an index on the queried column in the table, MySQL can quickly get to a place to search the data file without having to look at all the data, which will save a lot of time.

Advantages :

  1. All MySql column types (field types) can be indexed, that is, indexes can be set for any field;
  2. Greatly speed up data query;

Disadvantages :

  1. It takes time to create and maintain indexes, and the time spent will increase as the amount of data increases;
  2. Indexes also need space. We know that the data in the data table will also have a maximum online setting. If we have a large number of indexes, the index file may reach the online value faster than the data file;
  3. When adding, deleting, and modifying data in the table, the index also needs dynamic maintenance, which reduces the speed of data maintenance;

Principles of use :

  1. It is not enough to set an index for each field, nor is it that the more indexes the better, but it needs to be used reasonably.

  2. Avoid too many indexes on frequently updated tables, and create indexes on fields that are frequently used for queries.

  3. It is best not to use indexes for tables with a small amount of data, because due to the small amount of data, it may take less time to query all the data than to traverse the index, and the index may not produce optimization effects.

  4. Do not create indexes on columns (fields) with few values. For example, there are only two different values ​​of male and female in the "sex" field of the student table. On the contrary, there are many different values ​​​​on a field to build an index.

    Note: The index is implemented in the storage engine, that is to say, different storage engines will use different indexes

  • MyISAM and InnoDB storage engines : only support BTREE indexes, that is to say, BTREE is used by default and cannot be replaced.
  • MEMORY/HEAP storage engine : supports HASH and BTREE indexes

Indexes are divided into four categories : single-column index (ordinary index, unique index, primary key index), composite index, full-text index, spatial index,

  1. Single-column index : An index contains only a single column, but there can be multiple single-column indexes in a table;
  2. Ordinary index : the basic index type in MySQL, without any restrictions, allows to insert duplicate values ​​and null values ​​in the columns defining the index , purely for faster data query;
  3. Unique index : the value in the index column must be unique, but null values ​​are allowed ;
  4. Primary key index : It is a special unique index that does not allow null values ;
  5. Composite index : the index created on the combination of multiple fields in the table, the index will be used only when the left field of these fields is used in the query condition, and the leftmost prefix set is followed when using the composite index;
  6. Full-text index : it can only be used on the MyISAM engine, and it can only be used on CHAR, VARCHAR, TEXT type fields. It introduces the requirements and talks about what a full-text index is, which means passing a certain key in a bunch of text word, etc., you can find the record row to which the field belongs;
  7. Spatial index : Spatial index is an index established for fields of spatial data types. There are four spatial data types in MySQL, GEOMETRY, POINT, LINESTRING, and POLYGON. When creating a spatial index, use the SPATIAL keyword. Requirements, the engine is MyISAM, and the column to create a spatial index must be declared as NOT NULL;

Classification of Design Patterns

  • There are five creational patterns : factory method pattern, abstract factory pattern, singleton pattern, builder pattern , and prototype pattern.
  • There are seven structural modes : Adapter mode, Decorator mode, Proxy mode, Appearance mode, Bridge mode, Composition mode, Flyweight mode.
  • There are eleven types of behavioral patterns : strategy pattern, template method pattern, observer pattern , iterator pattern, chain of responsibility pattern, command pattern, memo pattern, state pattern, visitor pattern, mediator pattern, and interpreter pattern.

factory pattern

Advantages of simple factory pattern :

  1. The factory class contains the necessary logic to determine when to create an instance of which product. The client can be exempted from the responsibility of directly creating product objects, and can easily create corresponding products. The responsibilities of factories and products are clearly distinguished.
  2. The client does not need to know the class name of the specific product to be created, but only needs to know the parameters.
  3. Configuration files can also be introduced to replace and add new specific product categories without modifying the client code

The biggest disadvantage of the simple factory pattern :

  1. But when there is a new product to be added to the system, the factory class must be modified and the necessary business logic needs to be added to it, which violates the "opening and closing principle".
  2. In addition, in the simple factory model, all products are created by the same factory, the factory class has heavy responsibilities, and the business logic is more complicated. The coupling between specific products and factory classes is high, which seriously affects the flexibility and expansion of the system. sex.

Application scenario :

For relatively few product categories, consider using the simple factory pattern. The client using the simple factory pattern only needs to pass in the parameters of the factory class, and does not need to care about the logic of how to create objects, and can easily create the required products.

How to add new products without affecting existing code? The factory method pattern came into being. This article will introduce the second factory pattern - the factory method pattern .

The factory method pattern (FACTORY METHOD) is a commonly used class creation design pattern . The core spirit of this pattern is to encapsulate the changing part of the class , extract the personalized and changeable part as an independent class, and achieve decoupling through dependency injection , reuse and facilitate later maintenance and expansion purposes.

The Abstract Factory Pattern (Abstract Factory Pattern) belongs to the creational pattern in the design pattern and is used for the construction of product families . Abstract factory is the most abstract and general form of all factory patterns. Abstract factory refers to a factory pattern used when there are multiple abstract roles . The abstract factory pattern can provide an interface to the client, so that the client can create product objects in multiple product families without specifying the specific circumstances of the product

Proxy mode

The main advantages of the proxy mode :

  • The proxy mode plays an intermediary role between the client and the target object and protects the target object;
  • The proxy object can extend the functionality of the target object;
  • The proxy mode can separate the client from the target object, which reduces the coupling of the system to a certain extent and increases the scalability of the program.

Main disadvantages :

  • Proxy pattern will increase the number of classes in the system design
  • Adding a proxy object between the client and the target object will slow down the request processing speed;
  • Increased system complexity;

Application scenario :

  • Remote proxy, this method is usually used to hide the fact that the target object exists in different address spaces, and facilitate client access. For example, when a user applies for some network disk space, a virtual hard disk will be created in the user's file system, and when the user accesses the virtual hard disk, what the user actually accesses is the network disk space.
  • Virtual proxy, this method is usually used when the target object to be created is expensive. For example, it takes a long time to download a large image, and it cannot be completed in a short time due to the complexity of certain calculations. At this time, a small proportion of virtual agents can be used to replace the real object first, so as to eliminate the user's feeling that the server is slow.
  • A security proxy controls access to an object and can provide different users with different levels of access.

What is the difference between ArrayList and LinkedList?

Is it thread-safe : ArrayList and LinkedList are not guaranteed to be thread-safe

Underlying implementation : The underlying implementation of ArrayList is an array, and the underlying implementation of LinkedList is a doubly linked list.

Memory usage : There will be a certain waste of space in ArrayList, because each expansion is 1.5 times the previous one, and each element in LinkedList needs to store direct successors and direct predecessors as well as data, so the storage for each element is more than that of ArrayList Takes more space.

Application scenario : The underlying data structure of ArrayList is an array, so the time complexity when inserting and deleting elements will be affected by the position. The average time complexity is o(n). When reading elements, you can directly use the index Finding elements is not affected by the location, and the average time complexity is O(1), so ArrayList is more suitable for scenarios with more reading and less addition and deletion. The underlying data structure of LinkedList is a two-way linked list, so inserting and deleting elements is not affected by the position, the average time complexity is o(1), if it is inserted at a specified position, it is o(n), because it needs to be found before inserting At this position, the average time complexity of reading elements is o(n). Therefore, LinkedList is more suitable for scenarios with more additions and deletions and less reading and writing.

The difference between ArrayList and Vector

Same point :

  1. Both implement the List interface;
  2. The underlying data structure is an array;

Differences :

  1. Thread safety : Vector uses Synchronized to achieve thread synchronization, so it is thread-safe, while ArrayList is thread-unsafe.
  2. Performance : Since Vector uses Synchronized for locking, its performance is not as good as that of ArrayList.
  3. Expansion : Both ArrayList and Vector can dynamically adjust capacity according to needs, but ArrayList expands to 1.5 times the old capacity each time, and Vector expands to 2 times the old capacity each time .

The difference between Array and ArrayList

ArrayList is internally implemented by an array . ArrayList is a member of the Java collection framework class, which can be called a dynamic array. array is static, so once a data is created, its size cannot be changed. If you need an array that can redefine its size, you should use ArrayList, which is the basic difference between array and ArrayList.

Differences :

Array ArrayList
Implementation Native programming components or data structures A class from a Java collection class, an interface (Application programming interface)
Performance Memory usage and CPU time-consuming Memory usage and CPU time consumption are relatively small
Type Safety Does not support generics, type unsafe Support generics, type safety
Flexibility dynamic array, flexible static array
Primitives Support objects and primitive data types basic data type
Generics Generics are not supported supports generics
Iteration for、foreach Support Iteration
Supported Operations Does not support adding and removing operations Support add and delete operations
Size() vs length (size vs length) The length cannot be changed The initial length is 10, move one bit to the right, and expand the capacity by 1.5 times
Dimension Support for multidimensional arrays Multidimensional arrays are not supported

Same point :

  1. Data Structure (data structure) : Both allow objects to be stored, and all of them are index-based data structures, providing O(1) complexity to obtain an element, but if you query an element through binary search, you still need The complexity of log(N).
  2. Order (order) : Both array and ArrayList maintain the order of elements when adding elements.
  3. Search (find) : You can find an element by index;
  4. Null values ​​(empty value) : array and ArrayList both allow the storage of null values.
  5. Duplicates (repeated) : both array and Arraylis allow storing duplicate values.
  6. Zero-based Index (zero-based index) : Both array and ArrayList start from zero, that is, the first element starts from zero.

Set interface

Set interface: Store unordered (determined according to the hash value of the data), non-repeatable data --> "collection" in high school (no additional new methods are defined, all methods declared in Collection are used; to Set (Mainly refers to: the data added in HashSet, LinkedHashSet), its class must rewrite hashCode() and equals())

  • HashSet : as the main implementation class of the Set interface; thread-unsafe; can store null values
  • LinkedHashSet : As a subclass of HashSet; when traversing its internal data, it can be traversed in the order of addition. For frequent traversal operations, LinkedHashSet is more efficient than HashSet.
  • TreeSet : It can be sorted according to the specified attributes of the added objects, requiring objects of the same class - natural sorting (implementing the Comparable interface) and custom sorting (Comparator).

HashMap

The difference between HashMap in JDK1.7 and JDK1.8

  1. new HashMap(): The underlying layer does not create an array of length 16
  2. The underlying array of jdk 8 is: Node[], not Entry[]
  3. When the put() method is called for the first time, the underlying layer creates an array with a length of 16
  4. The underlying structure of jdk7 is only: array + linked list.

image-20210911212559126

  1. The underlying structure in jdk8: array + linked list + red-black tree. (The length of the linked list is greater than 8 and converted to a red-black tree)

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-uaaWRsxw-1632538378330)(Photo/image-20210911212612511.png)]

JDK1.7 JDK1.8 Advantages of JDK1.8
underlying structure array + linked list Array + linked list/red-black tree (linked list is greater than 8) Avoid affecting query efficiency due to too long single linked list, and improve query efficiency
Hash value calculation method 9 perturbations = 4 bit operations + 5 XOR operations 2 perturbations = 1 bit operation + 1 XOR operation The previous conflicting nodes can be evenly distributed to the new bucket (see the expansion section below for details)
Insert data method Head insertion method (first move the data at the original position to the last 1 bit, and then insert the data to this position) Tail insertion method (insert directly into the end of the linked list/red-black tree) Solve the problem of infinite loop caused by multithreading
Calculation method of storage location after capacity expansion Re-calculate the hash Original location or original location + old capacity Save the time to recalculate the hash value

Underlying code:

  • When forming a linked list, it is up and down (jdk7: new elements point to old elements. jdk8: old elements point to new elements)
  • When the number of elements at a certain index position of the array in the form of a linked list is > 8 and the length of the current array is > 64, all the data at this index position will be stored in a red-black tree instead.
  • TREEIFY_THRESHOLD: The length of the linked list in the Bucket is greater than the default value, converted into a red-black tree: 8
  • MIN_TREEIFY_CAPACITY: The minimum hash table capacity when the Node in the bucket is treed: 64
  • Iterator iterator interface

The expansion operation of HashMap

  1. DEFAULT_INITIAL_CAPACITYThe initial value is 16, DEFAULT_LOAD_FACTORthe load factor is 0.75, and thresholdthe threshold is load factor * capacity = 12;
  2. The resize() method is to call the resize() method to expand the capacity when the key-value pair in the hashmap is greater than the threshold or when it is initialized.
  3. Each expansion, the capacity is doubled before ;
  4. When expanding capacity, there is a judgment whether e.hash & oldCap is zero, which is equivalent to the modulo operation of the hash value on the length of the array. If it is equal to 0, the position will remain unchanged. If it is equal to 1, the position will be changed to the original position plus the old capacity.

Why does HashMap choose 0.75 as the default loading factor?

It is mainly a compromise between space utilization and query cost. ifIf the load factor is too high, the space utilization rate will increase, but the probability of hash collision will increase; if the load factor is too low, the capacity will be expanded frequently, the probability of hash collision will be reduced, but the space utilization rate will be reduced. Specifically why it is 0.75, not 0.74 or 0.76, this is a conclusion based on mathematical analysis (Poisson distribution) and industry regulations .

Why set the threshold of turning red-black tree in linked list to 8? Why not just use a red-black tree in the first place?

  1. Because the space occupied by the nodes of the red-black tree is twice that of the ordinary linked list nodes , but the time complexity of searching is low, the advantages of the red-black tree can only be reflected when there are too many nodes. As for why it is 8, it is a result of data analysis and statistics. The probability of the length of the linked list reaching 8 is very low. Considering the performance advantages and disadvantages of the linked list and red-black tree, the linked list larger than 8 is converted into a red-black tree.
  2. The linked list is converted into a red-black tree, in addition to the length of the linked list being greater than 8, the length of the array in the HashMap must be greater than 64. That is, if the length of the HashMap is less than 64 and the length of the linked list is greater than 8, it will not be converted into a red-black tree, but will be directly expanded .

Why are wrapper classes like String and Integer suitable as Key in HashMap?

  • These wrapper classes are final modified and immutable, which guarantees the immutability of the key, and there will be no
    difference in the hash value between putting and getting.
  • They have rewritten hashcode(), equal() and other methods internally.

What should I do if I use Object as the Key of HashMap? **

  1. Rewrite the hashCode() method, because the hash value needs to be calculated to determine the storage location
  2. Override the equals() method, because the uniqueness of the key needs to be guaranteed.

The difference between HashMap, ConcurrentHashMap and Hashtable

HashMap(JDK1.8) ConcurrentHashMap(JDK1.8) Hashtable
underlying implementation Array + linked list/red-black tree Array + linked list/red-black tree array + linked list
thread safety unsafe Security (Synchronized Modified Node node) Safe (Synchronized decorates the entire table)
efficiency high higher Low
expansion Initially 16, expand to 2n each time Initially 16, expand to 2n each time Initially 11, every expansion 2n+1
Whether to support Null key and Null Value There can be one Null key and multiple Null Values not support not support

Which are thread safe

ConcurrentHashMap、Vector、StringBuffer、hashTable、stack、enumration

Introduction to Spring

Advantages :

  • Low intrusive design, code pollution is extremely low.
  • Independent of various application servers, applications based on the Spring framework can truly realize the promise of Write Once, Run Anywhere.
  • Spring's IoC container reduces the complexity of business object replacement and improves the decoupling between components.
  • Spring's AOP support allows centralized management of some common tasks such as security, transactions, logs, etc., thus providing better reuse.
  • Spring's ORM and DAO provide good integration with third-party persistence layer frameworks and simplify the underlying database access.
  • The high openness of Spring does not force applications to completely depend on Spring, and developers are free to choose part or all of the Spring framework.

Spring's core mechanism

Manage Beans

The program mainly accesses the beans in the container through the Spring container. ApplicationContext is the most commonly used interface of the Spring container. This interface has the following two implementation classes:

  • ClassPathXmlApplicationContext: Search configuration files from the class loading path, and create a Spring container based on the configuration files.
  • FileSystemXmlApplicationContext: Search configuration files from the relative or absolute path of the file system, and create a Spring container based on the configuration files.

Beans in the Spring container

For developers, developers use the Spring framework to do two things: ① develop beans; ② configure beans. For the Spring framework, all it has to do is to create a Bean instance according to the configuration file, and call the method of the Bean instance to complete "dependency injection"-this is the essence of the so-called IoC.

dependency injection

Inversion of Control (IoC) is also called Dependency Injection (Dependency Injection). When a Java object (caller) needs to call a method of another Java object (dependent object), there are usually two approaches in the traditional mode:

  1. Original method: The caller actively creates the dependent object, and then calls the method of the dependent object.
  2. Simple factory mode: The caller first finds the factory of the dependent object, then actively obtains the dependent object through the factory, and finally calls the method of the dependent object.

There are two core functions of the Spring framework:

  • As a super factory, the Spring container is responsible for creating and managing all Java objects, which are called Beans.
  • The Spring container manages the dependencies between Beans in the container, and Spring uses a method called "dependency injection" to manage the dependencies between Beans.

Using dependency injection, you can not only inject ordinary property values ​​into beans, but also inject references to other beans. Dependency injection is an excellent way of decoupling, which allows beans to be organized together in configuration files rather than coupled together in a hard-coded way.

3 ways to create beans

Create a Bean instance using a constructor

Using a constructor to create a Bean instance is the most common case. If you do not use construction injection, the bottom layer of Spring will call the parameterless constructor of the Bean class to create an instance, so the Bean class is required to provide a parameterless constructor.

Using the default constructor to create a Bean instance, Spring performs default initialization on all properties of the Bean instance, that is, the values ​​of all basic types are initialized to 0 or false; the values ​​of all reference types are initialized to null.

Create beans using static factory methods

When using a static factory method to create a Bean instance, the class attribute must also be specified, but at this time the class attribute is not the implementation class of the specified Bean instance, but a static factory class. Spring knows which factory class to create the Bean instance through this attribute.

In addition, you need to use the factory-method attribute to specify the static factory method. Spring will call the static factory method to return a Bean instance. Once the specified Bean instance is obtained, the subsequent processing steps of Spring are exactly the same as the normal method to create the Bean instance. . If the static factory method requires parameters, use <constructor-arg.../>the element to specify the parameters of the static factory method.

Call the instance factory method to create the bean

There is only one difference between instance factory methods and static factory methods: calling a static factory method requires only the factory class, while calling an instance factory method requires a factory instance. When using the instance factory method, <bean.../>the element that configures the Bean instance does not need the class attribute, and the configuration instance factory method uses factory-beanthe specified factory instance.
When using the instance factory method to create Bean <bean.../>elements, you need to specify the following two attributes:

  • factory-bean: The value of this attribute is the id of the factory Bean.
  • factory-method: This attribute specifies the factory method of the instance factory.

If parameters need to be passed in when calling the instance factory method, use <constructor-arg.../>the element to determine the parameter value.

Spring-AOP

Why do you need AOP

AOP (Aspect Orient Programming) is aspect-oriented programming. As a supplement to object-oriented programming, it has become a relatively mature programming method. In fact, AOP has not been around for too long. AOP and OOP complement each other. Aspect-oriented programming decomposes the program running process into various aspects.

AOP is specially used to deal with cross-focus problems distributed in various modules (different methods) in the system. In JavaEE applications, AOP is often used to handle some system-level services with cross-cutting properties, such as transaction management, security checks, Cache, object pool management, etc., AOP has become a very common solution.

Implementing AOP with AspectJ

AspectJ is an AOP framework based on the Java language, which provides powerful AOP functions, and many other AOP frameworks have borrowed or adopted some of the ideas. It mainly includes two parts: one part defines how to express and define the grammatical specification in AOP programming. Through this set of grammatical specification, AOP can be easily used to solve the problem of cross-focus in the Java language; the other part is Tools section, including compilation and debugging tools, etc.

AOP implementations can be divided into two categories:

  1. Static AOP implementation: The AOP framework modifies the program during the compilation phase, that is, to enhance the target class and generate a static AOP proxy class, represented by AspectJ.
  2. Dynamic AOP implementation: The AOP framework dynamically generates AOP proxies during the running phase to enhance the target object, represented by Spring AOP.

In general, static AOP implementations have better performance, but require the use of special compilers. Dynamic AOP implementations are pure Java implementations, so no special compiler is required, but performance is usually slightly worse.

The basic concept of AOP

Some terminology about aspect-oriented programming:

  • Aspect: Aspect is used to organize multiple Advice, and Advice is defined in the aspect.
  • Joinpoint: A definite point in program execution, such as a method call, or an exception thrown. In Spring AOP, a join point is always a method invocation.
  • Enhanced processing (Advice): The enhanced processing performed by the AOP framework at a specific entry point. Handling types such as "around", "before" and "after"
  • Pointcut: A join point where enhanced processing can be inserted. In short, when a join point meets the specified requirements, enhancements are added to the join point, and the join point becomes a pointcut.

Spring's AOP support

The AOP proxy in Spring is generated and managed by Spring's IoC container, and its dependencies are also managed by the IoC container. In order to use support
in an application , Spring needs to add three libraries:@AspectJ

  • aspectjweaver.jar
  • aspectjrt.jar
  • aopalliance.jar

Guess you like

Origin blog.csdn.net/Royalic/article/details/120468816