10 basic java questions (fourth bullet)

31. What is multithreading? Where has it been applied?

A thread is an executable code segment. A thread must serve the process. A process has multiple threads. The main method is the main thread. The simultaneous execution of multiple threads is multithreading. The thread pool is to put the prepared threads in the thread pool. If you need to call a thread when processing a request, call it from the thread pool, and then put it back in the thread pool when it is used up, so as to prevent high concurrency and save resources. We do not involve too many threads in our current development. In fact, I personally Understanding that using multithreading is nothing more than to improve code execution efficiency, improve customer experience, and solve high concurrency, but if multithreading is used in the project, the later code maintenance is not very easy to maintain. We are now solving these high problems. Concurrency is recommended to use middleware to solve, redis, activeMQ, and solr, etc., and then talk about the principles of these middleware... you can cleverly avoid this multi-threading problem...

32. Have you ever encountered memory overflow or memory leak?

开发的时候遇到过,其实内存溢出指的是jvm内存溢出,通过调整堆,栈的大小来解决。代码中出现死循环或递归调用也会造成内存溢出,一般我们去调整一下他的内存大小就行,配置一下tomcat里的一个参数就能调整,一般造成内存溢出的原因是什么? 一方面有可能是代码中有长时间没有关闭的连接,或者是用IO读取完文件以后没有及时关闭,原来再使用hibernate的时候就遇到过内存溢出,就是因为没有及时的去关闭缓存造成的,还有就是代码问题,比如说写了死循环,程序出问题,递归自己调用自己的时候代码写的有问题都有可能造成内存溢出.我感觉内存泄漏和内存溢出差不多.

33. Talk about the GC garbage collection process

我们Java中的垃圾回收都是自动的,我们很少几乎不去手动的干预,我就是先说一下这个JVM中的的堆内存结构,他主要分为新生代和老年代,新生代就是用来存放刚被new出来的对象,一般情况下占堆的1/3空间,还有就是新生代中又分为3个区具体我也记不清是哪三个区了 反正老年代里存放咱们整个应用程序中生命周期长的内存对象,我原来再网上看过,我们的JVM垃圾回收算法我记得差不多有4种,标记清除法,复制算法,标记整理算法,分代收集算法,我就了解其中的两种,比如说 说清楚其中的两个就行,都说出来就有点太假了,根本不可能记不住那么多

The first is the mark-clear method. It is
divided into: mark, please remove.
Marking stage: directly mark useless objects in memory, and then directly reclaim the marked objects in the clearing stage;
disadvantages: memory fragments are formed, and some large objects cannot be found enough Space and trigger a new garbage collection action.
The second type: Copy algorithm:
Divide the memory into two pieces of equal size. When one piece of memory is used up, copy the surviving objects to the other piece, and then clean up the previous piece.
Disadvantage: waste too much memory (The use of the old age is inefficient) The
third type: mark-organize algorithm
moves all surviving objects to one end, and then directly cleans up the memory outside the boundary of this end.
Fourth type: generational collection algorithm: (current commercial virtual The machine uses this)
according to the object's mid-life, the memory is divided into several blocks. Generally, the Java heap is divided into the new generation and the old
generation.

34. JVM memory analysis? Explain the heap and stack? Explain the virtual machine?

When I was learning Java, I knew that the JVM memory structure has three main blocks: heap memory, method area and stack. Heap memory is the largest memory address in the JVM. It is mainly composed of the young generation, the old generation and the persistent generation. All new objects are stored in this area. The stack is the place where data is temporarily stored, and each thread contains a stack. The stack is stored in the first-level cache, and the access speed is relatively fast. Only basic data type objects and references to custom objects are stored in the stack. The data in each stack is private and cannot be accessed by other stacks. The method area stores the information of the class to be loaded (such as class name, modifier, etc.), static variables, constructors, final defined constants, fields and methods in the class and other information.
Declare and create objects
8. Access properties

35. How many ways are redis persistence?

There are two types of RDB and AOF. RDB is stored in a snapshot mode. This is also the default persistence mode of redis. The data is stored every other segment. The default is 15S. This can also be modified through the configuration file. , This storage method has higher performance.
Another is that AOF is an immediate persistence method. As long as the data changes, it will be saved to the hard disk. This method has higher data preservation integrity, but the performance is relatively poor. The main problem with RDB is that the server is down or out of power, which will cause data loss.

36. Have you encountered cache penetration and cache avalanche when using redis?

I have encountered it, and penetration is because redis caches and queries according to the key. If there is no corresponding value, you should go to the database to find it. If the value corresponding to the key does not exist, and the number of concurrent requests for the key is large, it will cause a lot of pressure on the back-end system. This is called cache penetration. When the cache server restarts or a large number of caches fail in a certain period of time, when it fails, it will also put a lot of pressure on the back-end system (such as DB), which is called "cache avalanche". The solution is to cache the empty query results and set different validity periods for the cache. Of course, the most effective method for redis disaster recovery is to build a cluster. After the cache expires, the number of threads that read the database and write the cache is controlled by locking or queueing. For example, only one thread is allowed to query data and write cache for a certain key, and other threads wait. Set different expiration times for different keys to make the time of cache invalidation as even as possible.

37. Can you talk about redis cluster?

Redis itself supports cluster operation redis_cluster, and redis also supports master-slave replication. There was a sentinel mode in the previous old version. When the master server is down, the slave server can be automatically converted to the master server. The redis cluster built by our company was built with ruby ​​scripts. We built a total of 6 servers, 3 masters and 3 backups. The principle of communication between them is that there is a ping-pong protocol for communication. They judge the status of a node. Judging by the voting election mechanism, if more than half of the judgments that an interface is down, the standby node will start. Yes, I will tell you the mechanism for storing data in it. In fact, this redis will build a cluster every time Each node stores a hash slot. Every time data is stored in it, redis will calculate a hash value based on the stored key value. Through this hash value, you can determine which hash slot should be stored in the end, and take it. This is also the case at the time.This is the redis cluster I know.

38. ActiveMQ message sending failure solution? Have you encountered any problems when using activeMQueue?

The first is to use the database to solve the problem:
how to cooperate? Isn’t this the ID of the sent product? Record the ID in the database before sending, and then set a status field, 0 means the message is being sent, and then the listener For consumption, change this field to 1 after the consumption is successful. Then there are several emergencies. The first case is a sudden power failure and then my message is first recorded in the database, and then his status is It’s not 0. Then we have a timer written in quartz, which will go to the database to run a batch every 5 minutes and send the message again with the data with the status of 0 until the consumption is successful.
The second solution:
Set the submission method when sending the message, change it to the manual submission method, and change the commit status to manual in the background. If the transmission is successful, then commit the manual submission method.

39. ActiveMQ usage scenarios

For ActiveMQ in our development, we used it to reduce the coupling degree of our project, mainly applied to such several scenarios, for example, our projects are now distributed, we can not implement all the functions in a module, Take the product management module as an example. When adding, modifying, or deleting products, other modules may also have related changes. For example, the search in the foreground module, the product information has changed, and the content in the index library should also be corresponding. At this time, we need to use a communication mechanism. The ActiveMQ type of framework is what we just need. When the product is operated, we can send a message saying that my product information has changed. Of course, we need to specify which one If the product changes, just send the corresponding product id. In the front-end module, we configure a message receiver. When a message is received, we can modify the index library.
Of course, in addition to adding products to synchronize the update index library, like the product details module, after the product is approved, I want to send a product id in the message queue to the message queue. There is a monitoring class in the pageService project that can generate a corresponding static page. There is also an order module that has been used before. When executing an order generation, bank deduction, successful deduction, and inventory reduction, this type of operation can be synchronized through ActiveMQ messages.

40. What is read-write separation?

In this project, MyCat is used to do it. The main library and the slave library are configured in mycat. When adding, deleting and modifying, the main library is operated, and the query is performed on the slave library. In fact, mysql itself is from 5.6 or later The version has the function of master-slave replication, they are data synchronization using the log file in mysql.

Guess you like

Origin blog.csdn.net/zhang_yuanbai/article/details/108701793