20230910java interview summary

1.How threadlocalmap and hashmap resolve hash conflicts

Hash conflict refers to multiple objects grabbing the same hash slot.
Hashmap uses the zipper method. The same slot maintains a linked list or a red-black tree.
Threadlocalmap uses the open address method and will detect (linear, quadratic, double).
The zipper method is suitable for solving Frequent hash conflicts occur (the amount of data is unpredictable).
Because the key of threadlocalmap is threadlocal, the chance of hash collision is very small.

2. The interfaces and functions of the bean life cycle that are open to the outside world

Life cycle: Preparation before creation -> Create instance -> Dependency injection -> Container cache -> Destroy instance
initializingBean: After the bean attribute is set, execute custom initialization logic
DisposableBean: When destroying the bean, execute custom cleanup logic#
@PostConstruct: Called when the bean is initialized
@PreDestroy: Called before the bean is destroyed
BeanPostProcessor: Modify the bean attribute value, wrap the bean object
InstantiationAwareBeanPostProcessor: Control the creation of the bean, process the attributes during attribute injection
BeanFactoryPostProcessor: After loading the bean definition, in the bean instance Modify the beanFactory configuration before transformation

3. How to modify the bean after initialization

1. Let your Bean class implement the InitializingBean interface and override the afterPropertiesSet method
2. @PostConstruct
3. Implement the BeanPostProcessor interface, which contains two methods: postProcessBeforeInitialization and postProcessAfterInitialization
4. If your Bean is created by implementing the FactoryBean interface, you You can return a modified Bean instance in the getObject method of FactoryBean.
5. Implement the BeanFactoryPostProcessor interface, and you can modify the Bean configuration before the Bean is instantiated after the container loads the Bean definition.

4. How to inherit annotations

1. The meta-annotation @inherited can be passed down, for classes
2. Based on class\interface inheritance, subclasses rewrite without annotations, for properties and methods

5.@Primary

When there are multiple implementation classes of an interface, spring does not know which
@Primary to inject: priority is given to the annotated object for injection. There is only one, and
@Qualifier is directly injected: the name is used after declaration.

6. Principles of declarative transactions

1. Spring starts, automatically registers transactions, creates post-processors of dynamic proxy beans and manages transaction-related beans. 2.
When creating a bean, find the @transational annotation method through the bean post-manager and store it in the cache
. 3. Create Dynamic proxy, create a dynamic proxy for annotated ones.
4. When calling a method or class with a @transational annotation, enter the transactionInterceptor, trigger the proxy logic, start the transaction, and process nested transactions and single transactions respectively. If an exception is thrown, rollback, if no exception is found, commit.

7. Propagation mechanism of declarative transactions

Ensure that a transaction can be controlled between multiple calling methods.
7
required: Default level, if the transaction exists, join, otherwise create a new one.
required_new: Create a new one, if it exists, suspend the current one.
supports: If it exists, join, If it does not exist, then it is not a transaction.
Mandatory: If it exists, join it. If it does not exist, throw an exception.
not_supports: Run as a non-transaction. If a transaction exists, suspend.
never: Run as a non-transaction. If it exists, throw an exception.
Nested: If a transaction exists, create a transaction as The current nested transaction; create a new one if there is none

8. Situations when declarative transactions fail

@transactional is applied to the non-public method
@transactional's propagation and uses the non-transactional
@transactional's rollbackFor setting error
. When A calls B, A does not declare B and B declares it, it will be invalid (only if it is called by someone other than the current class) )
The exception is caught by catch.
For example, the myisam engine does not support transactions.

9.Solutions for distributed transactions

Multiple systems collaborate at one operation, and a transaction involves the process of multiple systems collaborating through the network.
cap: consistent, available, partition tolerance

1.2pc: In the prepare phase, the coordinator notifies the preparation for submission, and feeds back the execution results but does not submit the transaction; in the commit phase, the notification can be submitted, and the transaction submission results are fed back, and if it fails, it will be rolled out (increasing the coordinator's overhead) 2. Transaction compensation tcc: try is
completed Check and reserve resources before submitting the transaction; confirm confirms that the execution business will formally execute the try resources; cancel cancels the release of the try resources by the business 3.
Message transaction: 2pc based on middleware, local transactions and message sending are placed in one transaction. Ensure that local operations and message sending are successful at the same time, asynchronous coordination is not conducive to high concurrency

10. Common optimization methods of mysql (storage, query, index)

Index: Primary key query will not return the table, avoid <> and != will result in the entire table, appropriate prefix index
query: avoid * specified fields, use join instead of subquery to reduce temporary table overhead, small tables drive large tables, pay attention to columns Operation, appropriately add redundant fields
Database structure: Use char18 instead of varchar18, simple data structure, use less text, appropriate tables and databases.
Joint index leftmost matching principle
Full query: log, explain troubleshooting and positioning

Guess you like

Origin blog.csdn.net/weixin_40986490/article/details/132790449