Stereotype interview day4

Iterator_FailFast和Iterator_FailSafe

FailFast: Once it is found that other people are modifying while traversing, an exception will be thrown immediately (Note: debug mode is traversing and adding an element at this time, and the exception will be thrown after this round of traversal)

FailSafe: When it is found that other people are modifying while traversing, there should be a coping strategy, such as sacrificing consistency to let the entire traversal run to completion (Note: the debug mode is traversing and adding an element at this time, the result is only traversing the elements in the previous collection, just debug The elements added by the mode are not traversed) 

The difference between filters and interceptors 

First: The running order is different. The filter is run after the Servlet container receives the request but before the Servlet is called, while the interceptor runs after the Servlet is called but before the response is sent to the client.

Second: The configuration method is different. The filter is configured in web.xml, while the interceptor is configured in the spring configuration file or using annotations.

Third: Filter depends on the Servlet container, while Interceptor does not depend on the Servlet container 

Fourth: Filter can only operate on request and response in the filter, while interceptor can operate on request, response, handler, modelAndView, and exception, which is equivalent to having more interceptors for components in the springmvc ecosystem.

Microservice understanding

Microservice is an architectural style. We can divide an application into a small loosely coupled service. Each service runs in its own process and communicates through a lightweight communication mechanism. Each service All can be independently deployed, independently expanded, and independently updated, thereby improving the scalability, maintainability, and testability of the application. Since microservice is a design style for application architecture under distributed architecture, we will face some complex problems of service governance under distributed architecture, so spring officially provides a set of springcloud solutions. Help us quickly realize the landing of microservice technology solutions

Why Companies Ban @Transactional Declarative Transactions 

First: Add @Transactional declarative transactions to methods. If there are more time-consuming operations in a method, it is easy to cause long transaction problems, and long transactions will bring lock competition, affect performance, and also cause The connection pool of the database is exhausted, which affects the normal execution of the program

Second: If the method has nested calls, and the nested method also declares the @Transactional transaction, then the call behavior of transaction nesting will appear at this time, which will easily cause transaction confusion and abnormal program running results

Third: Transactional declarative transaction is to put the transaction control logic in the annotation. If the complexity of the project increases, the transaction control may become more complicated, resulting in a decrease in the readability and maintainability of the code, so in order to avoid this For this kind of problem, some companies will recommend the use of programmatic transactions, which can control the scope of transactions more flexibly, reduce the locking time of transactions, and improve system performance

How Mybatis paginates 

First: Add the pagination keyword provided by the database directly on the select statement, and then pass the current page and the number of items displayed on each page in the application

Second: Use the RowBounds object provided by Mybatis to implement memory-level paging

Third: Based on the Interceptor interceptor in Mybatis, dynamically splicing pagination keywords before the execution of the select statement

How many objects did new String("abc") create?

Explanation: First of all, there is a new keyword in this code . This keyword is to instantiate a string object in the heap memory according to the loaded system class String when the program is running, and then in the construction method of this String. , a string of "abc" is passed, because the string member variable in String is a final modification, so it is a string constant, so then the JVM will take the literal "abc" to the string constant In the pool , try to find a String object reference corresponding to it . If you can't get it, you will go to the heap memory to create a "abc" String object, and save the reference in the string constant pool . If there is another A definition of the literal "abc", because there is already a reference to the literal "abc" in the string constant pool, so you only need to obtain the corresponding reference from the constant pool, no need to create it

Answer: 1. If the string constant abc does not exist, two objects will be created, namely the string constant abc and the instance object new String 2. If the string constant abc exists, only one object will be created, namely String this object

The difference between Synchronized and lock

First: From a functional point of view, both lock and synchronized are tools used in java to solve thread safety problems

Second: From the perspective of characteristics, first of all, synchronized is a synchronization keyword in java, and Lock is an interface provided in the juc package. This interface has many implementation classes, including the implementation of a reentrant lock such as ReentrantLock. In fact, Synchronized can control the strength of the lock in two ways, one is to decorate the synchronized keyword in the method layer, the other is to decorate the code block, and we can control the lock through the life cycle of the synchronized lock object Scope of action. For example, if the lock object is a static object or a class object, then the lock belongs to the global lock. If the lock object is an ordinary instance object, then the scope of the lock depends on the life cycle of the instance, and the granularity of the lock in the lock is It is determined by the lock() method and unlock() method provided in it. As wrapped between the two methods, thread safety can be guaranteed. The scope of the lock depends on the life cycle of the lock instance. Lock is more synchronized than Synchronized. Higher flexibility, the lock can independently decide when to lock and when to release the lock, just call the two methods of lock() and unlock()

Third: lock also provides a non-blocking competitive lock method trylock(), which can tell the current thread whether other threads are using the lock by returning true/false, and synchronized is a keyword, so it cannot go The method of implementing non-blocking competing locks. In addition, the release of synchronized locks is passive. It will be released only after the execution of the synchronized synchronization code block is completed or when the code is abnormal. Finally, lock provides a mechanism for fair locks and unfair locks. , fair lock means that when threads compete for lock resources, if other threads are already queuing or waiting for the lock to be released, then the thread currently competing for the lock cannot go to the queue, and non-fair lock means that no matter whether there are threads waiting in line for the lock, it will To try to compete for a lock, synchronized only provides an implementation of an unfair lock

Fourth: In terms of performance, synchronized and lock are not much different in performance, but there will be some differences in implementation. Synchronized introduces biased locks, lightweight locks, heavyweight locks, and lock upgrade mechanisms to achieve Lock optimization, while the lock uses the spin lock method to achieve performance optimization

The difference between @Autowired and @Resource annotations

These two annotations are used to implement Bean dependency injection in the Spring ecosystem

First of all, @Autowired is an annotation provided in spring. It defaults to dependency injection according to the type. There is a required attribute in it, which is true by default, which means that an injection of a bean instance is mandatory. When the application starts, if it does not exist in the IOC container For the corresponding type of bean, an error will be reported when starting, but if we do not want to achieve automatic injection, we can set the property to false. Secondly, if there are multiple bean instances of the same type in the springIOC container, because it is based on the type Inject bean instances, so when the spring container starts, it will prompt an error meaning that only one single instance bean can be injected originally, but there will be multiple ones in the IOC container, resulting in injection failure. We can use @Primary and @Qualifier annotations to Solution, primary means the main bean. When there are multiple beans of the same type, a bean that declares the @Primary annotation is preferred. The qualifier is similar to conditional filtering. It finds the target bean that needs to be assembled according to the name of the bean

@Resource is an annotation provided in JDK, but Spring provides a knowledge of this annotation function in implementation. It is the same as Autowired annotation. The biggest difference is that Resource can support both ByName and ByType injection methods. If you use name, Spring Dependency injection will be performed according to the name of the bean. If type is used, Spring will implement dependency injection according to the type. Assuming that neither of them is configured, it will first match according to the defined attribute name. If no match is successful, then Match according to the type, if the two do not match, an error will be reported

Summary: 1. Autowired is matched according to type, while Resource can be matched according to name and type, the default is name

2. Autowired is an annotation defined in Spring, and Resource is an annotation defined in the JSR 250 specification, but Spring provides support for JSR 250

3. If Autowired needs to support name matching, you need to use Primary and Qualifier annotations to achieve support

IOC workflow

What are IOCs? The full name of IOC is inversion of control. Its core idea is to hand over the management authority of the object to the container. If the application needs to use an instance of an object, it can be obtained directly from the IOC container. The advantage of this design is that it reduces the The coupling between objects in the program makes the entire architecture of the program more flexible

Bean declaration method: Spring provides many ways to declare a bean, such as through the <bean> tag in XML or through @Service annotation or through @Configuration configuration class through @Bean annotation to declare, etc., then Spring starts Time to parse these beans and save them in the IOC container

The first stage is the initialization stage of the IOC container. In this stage, the bean definition is generated after parsing and loading according to the bean declaration method defined in the program such as XML or annotation, and then the bean definition is registered in the IOC container through annotation or XML. The declared bean will be parsed to get a bean definition entity, which will contain some definitions and basic attributes of the bean, and finally save the bean definition to a map collection to complete the initialization of IOC. The role of IOC is to register these Bean definition information is processed and maintained, which is a core of IOC container control propagation

The second stage is to complete bean initialization and dependency injection. The first is to initialize the singleton bean that does not set the lazy-init attribute through reflection, and the second is to complete the dependency injection of the bean

The last stage is the use of beans. Usually, we will obtain an instance of a specified bean from the IOC container through Autowired annotations or BeanFactory.getBean(), and set lazy-init attributes and an instantiation of non-singleton beans. , is to call the bean initialization method to complete the instantiation every time the bean object is obtained, and SpringIOC will not manage these beans

ArrayList expansion 

ArrayList is a storage container of an array structure. By default, the length of the array is 10. Of course, we can also specify the initial length when constructing the ArrayList object. Then, as data is continuously added to the ArrayList in the program, when adding When the number of data reaches 10, there is not enough capacity in the ArrayList to store subsequent data. At this time, ArrayList will trigger automatic expansion. First, create a new array whose length is 1.5 times the length of the original array. , and then use the Arrays.copyOf method to copy the data in the old array to the new array. After the expansion is completed, add the elements that need to be added to the new array to complete the process of dynamic expansion.

Advantages and disadvantages of indexing

advantage

1. It can greatly speed up the query speed of data , especially when the data table is very large, the query speed without index will be very slow

2. Using the index can reduce the IO cost of the database . When the index hits, MySQL does not need to scan the entire data table, but can locate the corresponding row

3. The index can ensure the uniqueness of the data in the data table , which is especially important for some occasions that require unique data, such as primary key index, unique index, etc.

4. If you often need to sort a field, you can use the index to optimize the sorting operation, thereby improving query efficiency

shortcoming

1. The index needs to occupy storage space . If the data table is very large, the index may occupy a lot of disk space

2. Every time you insert, update or delete data, MySQL needs to update the index, which will slow down the writing speed

3. The index needs to occupy memory. When the data table becomes very large, the index may exceed the memory limit, resulting in performance degradation ( increasing system overhead )

4. If the index is not designed properly, it may lead to a decrease in query performance . For example, if too many indexes are used, the query optimizer may not be able to select the optimal index, resulting in degraded query performance

5 ways to enable multithreading 

Inherit the Thread class: create a new class, inherit Java's built-in Thread class, and then override the run() method, then create an instance of this class, and call the start() method to start the thread

Implement the Runnable interface: Create a new class, implement the built-in Runnable interface in Java, then rewrite the run() method, then create an instance of the Thread class, pass the Runnable object as a parameter to the Thread constructor, and call the start() method to start the thread

Use ExecutorService and Executors classes: Java provides the Executor Service interface and Executors class to create and manage thread pools more conveniently. You can use Executors.newFixedThreadPool() or others to create thread pools, and then submit tasks through submit() or execute() Execute to the thread pool

Use Callable and Future interfaces: this method can get the return result

Use Fork Join Pool and Recursive class or Recursive Action class

interview experience speech

1. About salary

I saw that the salary range of your company is 6k to 8k, so I have experience in related industries in the same position so I can quickly start, hoping to get a basic salary of 8k

2. Whether to accept overtime 

I will improve work efficiency and try to get all the work done before leaving get off work to reduce unnecessary overtime, but at the same time I am fully prepared and go all out for the goal.

3. The reason for leaving the previous company

I want to continue to cultivate and develop this major, but I adjusted the product business direction behind the company, which is not in line with my career plan

4. How long does it take to hand over the resignation

Now the job position has a great impact on the company. In order to properly and stably hand over, it will take about half a month. Even if I am about to leave, I still have to be responsible for my last job.

5. What else do you want to ask?

I would like to know how many people are in this position in our company. If I am lucky enough to be a member of the company, what can I prepare in advance?

Guess you like

Origin blog.csdn.net/bubbleJessica/article/details/130675736