In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "interview questions and answers in JAVA development" with detailed content, clear steps and proper handling of details. I hope that this article "what interview questions and answers are in JAVA development" can help you solve your doubts.
1. Introduce yourself, your own projects and technical areas
Open question
2. Monitoring in the project: what are the common monitoring indicators?
Answer: CPU, memory, IO and so on. It is recommended to download a nmon tool, which contains various indicators.
Database: Mysql (cache hits, indexes, single SQL performance, database threads, datapool connections)
Middleware: 1. Message 2, load balancing 3, cache (including number of threads, number of connections, log).
Network: throughput, throughput
Application: jvm memory, log, Full GC frequency
3. What are the technologies involved in micro-services and the issues that need to be paid attention to?
4. What do you know about the registry?
Answer: Consul, Eureka, ZooKeeper
5. Do you know the reliability of consul?
6. Have you ever delved into the mechanism of consul? Have you compared it with other registries?
7. Spring is often used in projects. Do you understand the principle of Spring? The principle of AOP and IOC
Answer: (1). IoC (Inversion of Control) means that the container controls the relationship between program objects, rather than being directly controlled by program code in traditional implementations. The control is transferred from the application code to the external container, and the transfer of control is called reversal. For Spring, it is Spring that controls the lifecycle of objects and the relationship between objects; IoC has another name-"dependency injection (Dependency Injection)". In terms of name, the so-called dependency injection means that the dependency between components is determined by the container at run time, that is, the container injects some dependency into the component dynamically.
(2)。 In the way Spring works, all classes will register in the spring container to tell spring what it is and what you need, and then spring will give you what you want when the system is running properly, and give you what you need at the same time. The creation and destruction of all classes are controlled by spring, that is, the life cycle of an object is no longer controlled by the object that references it, but by spring. For a specific object, it used to control other objects, but now all objects are controlled by spring, so this is called control inversion.
(3)。 In the operation of the system, it dynamically provides an object with other objects it needs.
(4)。 The idea of dependency injection is realized through reflection mechanism. When a class is instantiated, it invokes the set method in the class through reflection to inject the class attributes saved in advance in HashMap into the class. To sum up, in the traditional method of object creation, the instance of the callee is usually created by the caller, while the work of creating the callee in Spring is done by Spring, and then the caller is injected, which is the so-called dependency injection or control inversion. There are two ways of injection: dependency injection and setting injection; the advantages of IoC: reduce the coupling between components, reduce the complexity of replacement between business objects, so that it can manage objects flexibly.
AOP (Aspect Oriented Programming)
(1)。 AOP aspect-oriented programming is based on IoC and is a useful supplement to OOP.
(2)。 AOP uses a technique called "crosscutting" to unravel the interior of encapsulated objects and encapsulate common behaviors that affect multiple classes into a reusable module called "Aspect", or aspect. To put it simply, the so-called "aspect" is to encapsulate the logic or responsibility that has nothing to do with the business but is jointly invoked by the business module, such as logging, which helps to reduce the repetitive code of the system and reduce the coupling between modules. and conducive to future maneuverability and maintainability.
(3)。 AOP represents a horizontal relationship, comparing the "object" to a hollow cylinder, which encapsulates the properties and behavior of the object, while the aspect-oriented programming method is to cut the cylinder in section form and selectively provide business logic. And the cut section is the so-called "aspect". Then it restores these cut sections with a brilliant hand, leaving no trace, but achieves the effect.
(4)。 The technology to realize AOP is mainly divided into two categories: one is to use dynamic proxy technology to decorate the message by intercepting the message to replace the execution of the original object behavior, and the other is to use static weaving to introduce specific syntax to create "aspects", so that the compiler can weave the code related to "aspects" during compilation.
(5)。 Spring implements AOP:JDK dynamic proxy and CGLIB proxy JDK dynamic proxy: its proxy object must be the implementation of an interface, which completes the proxy to the target object by creating an interface implementation class at run time; the two core classes are InvocationHandler and Proxy. CGLIB proxy: the implementation principle is similar to the JDK dynamic proxy, except that the proxy object it generates at run time is a subclass that extends for the target class. CGLIB is an efficient code generation package, and the bottom layer relies on ASM (open source java bytecode editing class library) to operate bytecode, which has better performance than JDK; packages asm.jar and cglib.jar need to be introduced. Using AspectJ injection aspects and @ AspectJ annotation drivers, the underlying aspects are actually implemented through dynamic proxies.
(6)。 AOP usage scenarios:
Authentication permission check
Caching caching
Context passing content delivery
Error handling error handling
Lazy loading delayed loading
Debugging debugging
Logging, tracing, profiling and monitoring logging, tracking, optimization, calibration
Performance optimization performance optimization, efficiency check
Persistence persistence
Resource pooling resource pool
Synchronization synchronization
Transactions transaction management
In addition, the implementation of Filter and the interceptor of struts2 are the embodiment of the idea of AOP.
8. Apart from automatic configuration, what is the difference between Spring Boot and traditional Spring?
Provides a more concise way for the development of the Spring ecosystem, providing many non-functional features, such as embedded Server,Security, statistics, health check, external configuration, etc., mainly reflected in the following points:
1.Spring Boot can build independent Spring applications
two。 Embedded containers such as Tomcat,Jetty and Undertow, which means you can run directly without having to deploy.
3. There is no need to configure a bunch of tedious xml files like Spring.
4. Spring can be configured automatically. SpringBoot changes the original XML configuration to Java configuration, changes bean injection to annotation injection (@ Autowire), and condenses multiple xml and properties configurations into one appliaction.yml configuration file.
5. Provides some existing features, such as measurement tools, form data validation, and some external configuration and other third-party functions
6. Integrate common dependencies (development libraries such as spring-webmvc, jackson-json, validation-api, tomcat, etc.) and provide POM that simplifies the configuration of Maven. When we introduce core dependencies, SpringBoot introduces other dependencies on its own.
9. How much does Spring Cloud know?
Spring Cloud is an ordered collection of frameworks. It makes use of the development convenience of Spring Boot to skillfully simplify the development of distributed system infrastructure, such as service discovery registration, configuration center, message bus, load balancing, circuit breaker, data monitoring and so on. It can be started and deployed with one button with the development style of Spring Boot. Spring Cloud does not repeat manufacturing wheels, it just combines the more mature service frameworks developed by various companies that can stand the actual test, and shields the complex configuration and implementation principles through Spring Boot style re-encapsulation, and finally leaves a set of distributed system development kits that are easy to understand, easy to deploy and easy to maintain.
10. The life cycle of Spring Bean
A Bean from creation to destruction, if BeanFactory is used to generate and manage Bean
The Bean in the context of Spring is similar, as follows
1. Instantiate a Bean--, which is often called new.
2. Configure the instantiated Bean according to the Spring context, that is, IOC injection
3. If the Bean has implemented the BeanNameAware interface, the setBeanName (String) method it implements will be called. Here, the id value of Bean in the Spring configuration file is passed.
4. If the Bean has already implemented the BeanFactoryAware interface, the setBeanFactory that will be called (setBeanFactory (BeanFactory) passes the Spring factory itself (you can get other Bean in this way, just configure a normal Bean in the Spring configuration file)
5. If the Bean has implemented the ApplicationContextAware interface, the setApplicationContext (ApplicationContext) method will be called, passing in the Spring context (the same way can also implement the content of step 4, but it is better than 4, because ApplicationContext is a subinterface of BeanFactory, and there are more implementation methods)
6. If the Bean is associated with a BeanPostProcessor interface, the postProcessBeforeInitialization (Object obj, String s) method will be called. BeanPostProcessor is often used as a change to the Bean content, and since this is the method that is called at the end of Bean initialization, it can also be applied to memory or caching techniques.
7. If Bean configures the init-method property in the Spring configuration file, the initialization method of its configuration will be called automatically.
8. If the Bean is associated with a BeanPostProcessor interface, the postProcessAfterInitialization (Object obj, String s) method will be called,
Note: after the above work is completed, you can apply this Bean, so this Bean is a Singleton, so in general, we will call the Bean of the same id in an instance with the same content address. Of course, we can also configure non-Singleton in the Spring configuration file, which we will not repeat here.
9. When Bean is no longer needed, it goes through the cleaning phase. If Bean implements the interface DisposableBean, the destroy () method that it implements will be called.
10. Finally, if the destroy-method attribute is configured in the Spring configuration of this Bean, the destroy method of its configuration will be called automatically.
In addition, what we describe here is the life cycle of applying Spring context Bean. If the factory that applies Spring is BeanFactory, then remove step 5 and Ok.
11. The difference between HashMap and hashTable?
Difference: Hashtable is thread-safe and inefficient
Hashtable supports neither Null key nor Null value. The put () method of Hashtable is explained in the comments
The default initial size of Hashtable is 11, and then with each expansion, the capacity changes to the original 2n+1.
The default initialization size for HashMap is 16. After each expansion, the capacity was doubled.
Hashtable needs to perform a division operation when calculating the position of an element, and the division operation is time-consuming.
In order to improve the computational efficiency, HashMap fixed the size of the hash table to the power of 2, so that there is no need to do division, only bit operation. Bit operations are much more efficient than division.
HashMap inherits from the AbstractMap class, while HashTable inherits from the Dictionary class. However, they all implement map, Cloneable (replicable) and Serializable (serializable) interfaces at the same time.
12. The hashcode method of Object has been rewritten. Should I change the equals method?
No, there are two methods equals and hashCode in the Ojbect class, both of which are used to compare whether two objects are equal. If the two objects are equal (equal), then they must have the same hash code.
Even if two objects have the same hash value (hash code), they are not necessarily equal
Overriding the equals () method requires overriding hashCode (), but overriding the hashcode method does not necessarily require overriding the equals method.
13. Scenarios where Hashmap thread is not safe
Thread safety with ConcurrentHashMap
Hashmap threads are not safe in multithreading
First of all, the size in hashmap is not modified with the volatile keyword, which means that it is not a variable visible in memory. When a thread operates the data, it usually copies a copy of the variable from the main memory. After the operation is completed, the value of the size is written back to the main memory size.
Thread unsafety should be one of the concurrency problems, which is a relatively advanced problem. At this time, the problem is not limited to the code level, many times it needs to be analyzed together with JVM.
14. What should I do if the online service CPU is very high? What measures are there to find the problem?
Locate the stack information with the problem and troubleshoot the specific problem
1. Top command: Linux command. You can view real-time CPU usage. You can also view CPU usage in the recent period of time.
2. Ps command: Linux command. Powerful process status monitoring commands. You can view the current CPU usage of the process and the threads in the process. Sampling data that belongs to the current state.
3. Jstack: the command provided by Java. You can view the current thread stack running of a process. Based on the output of this command, you can locate the current running state of all threads of a process, running code, whether it is deadlocked, and so on.
4. Pstack:Linux command. You can view the current thread stack running of a process
15. Which thread pools are there in JDK? I talked about the thread pool by the way.
JUC provides the scheduler object Executors to create thread pools. There are four thread pools that can be created.
1. NewFixedThreadPool creates a thread pool with a specified number of worker threads. Each time a task is submitted, a worker thread is created, and if the number of worker threads reaches the initial maximum of the thread pool, the submitted task is stored in the pool queue.
2. NewCachedThreadPool creates a cacheable thread pool. The characteristics of this type of thread pool are:
1)。 There is almost no limit to the number of worker threads created (in fact, there is also a limit, the number is Interger. MAX_VALUE), which provides the flexibility to add threads to the thread pool.
2)。 If the task is not submitted to the thread pool for a long time, that is, if the worker is idle for a specified period of time (the default is 1 minute), the worker will automatically terminate. After termination, if you submit a new task, the thread pool recreates a worker thread.
3. NewSingleThreadExecutor creates a single-threaded Executor, that is, only a unique worker thread is created to execute the task. If this thread ends abnormally, another thread will replace it to ensure sequential execution (I think this is its feature). The most important feature of a single worker thread is that it can ensure that each task is executed sequentially, and that no multiple threads are active at any given time.
4. NewScheduleThreadPool creates a fixed-length thread pool and supports scheduled and periodic task execution, similar to Timer. (this thread pool principle is not fully understood yet.)
15. What are the common methods of SQL optimization
Reduce the use of functions in query conditions and avoid full table scanning
Reduce unnecessary table joins
Some business logic of data operation can be implemented in the application layer.
You can use with as
Avoid using cursors as much as possible, because cursors are inefficient
Don't make the SQL statement too complicated
Cannot cycle through the query
Replace in with exists
Don't be too tangled with the relationship between tables
Charindex or like [0-9] is used instead of%.
The table associated with inner can be checked first, and then associated with the table of leftjoin.
You can split the table association data, that is, find out the core data first, and then check other data through the core data, which will be much faster.
Optimize with reference to SQL execution order
Aliases are used when tables are associated, which can also improve efficiency
Use the view to index the view for optimization
In the form of a data warehouse, a separate table is established to store the data, and the data is updated periodically according to the timestamp. The data set associated with multiple tables is extracted and stored in one table, and the query is queried by a single table, which improves the efficiency of the query.
In order to optimize the query, we should try our best to avoid full table scanning. We should first consider establishing indexes on the columns involved in where and order by.
Try to avoid judging the null value of a field in the where clause, otherwise it will cause the engine to abandon the use of the index and perform a full table scan, such as:
Select id from t where num is null
You can set the default value of 0 on num to ensure that there is no null value for the num column in the table, and then query it like this:
Select id from t where num=0
16. The use of the! = or operator in the where clause should be avoided as far as possible, otherwise the engine will abandon the use of indexes and perform full table scans
17. Order of SQL index and order of fields
18. Check whether SQL uses an index? (what tools do you have)
Just add EXPLAIN before the select statement
19. What's the difference between TCP and UDP? How to achieve reliability in TCP data transmission process?
UDP (User Data Protocol, user Datagram Protocol) is the protocol corresponding to TCP. It belongs to the TCP/IP protocol family.
1) in order to ensure the reliable delivery of the packet, the sender must keep the sent packet in the buffer.
(2) and start a timeout timer for each packet sent
(3) if a reply message is received from the other party before the timer expires (it may be a reply to this packet or a reply to a subsequent packet), release the buffer occupied by the packet.
(4) otherwise, the packet is retransmitted until the number of replies or retransmissions exceeds the specified maximum number.
(5) after receiving the data packet, the receiver first carries out the CRC check, if it is correct, it gives the data to the upper layer protocol, and then sends a cumulative reply packet to the sender, indicating that the data has been received. If the receiver happens to have data to be sent to the sender, the response packet can also be piggybacked in the data packet.
20. Tell me about the sorting algorithm you know
Common internal sorting algorithms are: insert sort, Hill sort, select sort, bubble sort, merge sort, quick sort, heap sort, cardinality sort and so on.
Find the median of an array?
Find the median by binary search
The basic idea is: assuming that AR1 [I] is the median after merging, then ar1 [I] is greater than the number of pre-j=n-i-1 in ar1 [] and greater than the number of pre-IRUL in ar2 []. By comparing AR1 [I] with AR2 [j] and ar2 [juni1], the binary search continues on the left side of ar1 [I] or on the right side of ar1 [I]. For the two arrays ar1 [] and ar2 [], do a binary search in ar1 [] first. If the median is not found in ar1 [], continue to look in ar2 [].
Algorithm flow:
1) get the middle number of the array ar1 [], assuming that the subscript is I.
2) calculate the subscript j _ j = n-i-1 corresponding to the array ar2 []
3) if ar1 [I] > = ar2 [j] and ar1 [I]
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.