java interview FAQ! Prepare for spring recruitment interview must see!


Interview questions

Java Foundation

  1. How many types of IO streams are there in Java?
  2. What is the difference between BIO, NIO and AIO?
  3. What are the common methods of Files?


  1. What is the difference between parallelism and concurrency?
  2. What is the difference between threads and processes?
  3. What is a daemon thread?
  4. What are the ways to create threads?
  5. What's the difference between runnable and callable?
  6. What are the states of threads?
  7. What's the difference between sleep() and wait()?
  8. What's the difference between notify() and notifyAll()?
  9. What are the ways to create a thread pool?
  10. What's the difference between a thread's run() and start()?
  11. What are the states of thread pools?
  12. What is the difference between the submit() and execute() methods in the thread pool?
  13. How to ensure the safe operation of multithreading in Java programs?
  14. What is the upgrade principle of multithreaded lock?
  15. What is deadlock?
  16. How to prevent deadlock?
  17. What is ThreadLocal? What are the usage scenarios?
  18. Talk about the underlying implementation principle of synchronized?
  19. What is the difference between synchronized and volatile?
  20. What's the difference between synchronized and Lock?
  21. What is the difference between synchronized and ReentrantLock?
  22. Talk about the principle of atomic?

spring/spring MVC

  1. Why use spring?
  2. Explain what aop is?
  3. Explain what ioc is?
  4. What are the main modules of spring?
  5. What are the common injection methods of spring?
  6. Are bean s in spring thread safe?
  7. How many bean scopes does spring support?
  8. How does spring automatically assemble bean s?
  9. What are the implementation methods of spring transaction?
  10. What about spring's transaction isolation?
  11. Tell me about the spring mvc running process?
  12. What are the components of spring mvc?
  13. @What is the role of RequestMapping?
  14. @What is the role of Autowired?
    4, spring/spring Cloud
  15. What is spring boot?
  16. Why spring boot?
  17. What is the spring boot core configuration file?
  18. What are the types of spring boot configuration files? What's the difference between them?
  19. How can spring boot implement hot deployment?
  20. What's the difference between jpa and hibernate?
  21. What is spring cloud?
  22. What is the function of spring cloud circuit breaker?
  23. What are the core components of spring cloud?
    5, Mybatis
  24. What is the difference between #{} and ${} in mybatis?
  25. How many paging modes does mybatis have?
  26. Is RowBounds a one-time query for all results? Why?
  27. What is the difference between mybatis logical paging and physical paging?
  28. Does mybatis support deferred loading? What is the principle of delayed loading?
  29. Talk about the first level cache and second level cache of mybatis?
  30. What are the differences between mybatis and hibernate?
  31. What executors does mybatis have?
  32. What is the implementation principle of mybatis paging plug-in?
  33. How does mybatis write a custom plug-in?
    6, kafk, Zookeeper
  34. Can kafka be used separately from zookeeper? Why?
  35. How many data retention strategies does kafka have?
  36. kafka sets both 7 days and 10G to clear data. By the fifth day, the message reaches 10G. How will kafka handle it at this time?
  37. What causes kafka to run slowly?
  38. What should we pay attention to when using kafka cluster?
  39. What is zookeeper?
  40. What are the functions of zookeeper?
  41. How many deployment modes does zookeeper have?
  42. How does zookeeper ensure the state synchronization of master and slave nodes?
  43. Why should there be a master node in a cluster?
  44. There are three servers in the cluster, and one of them is down. Can zookeeper still be used at this time?
  45. Talk about zookeeper's notification mechanism?
    7, Redis
  46. What is Redis? What are the usage scenarios?
  47. What are the functions of redis?
  48. What is the difference between redis and memecache?
  49. Why is redis single threaded?
  50. Under what circumstances will redis slow down?
  51. What is cache penetration? How?
  52. What data types does redis support?
  53. What are the Java clients supported by redis?
  54. What are the differences between Jedis and redisson?
  55. How to ensure the consistency of cache and database data?
  56. How many ways can redis be persisted?
  57. How does redis implement distributed locks?
  58. What are the defects of redis distributed lock?
  59. How does redis optimize memory?
  60. What are the strategies to eliminate redis?
  61. What are the common performance problems of redis? How to solve it?
    8, JVM
  62. What are the main components of Jvm? And its role?
  63. What about the Jvm runtime data area?
  64. What's the difference between stacks?
  65. What are queues and stacks? What's the difference?
  66. What is the parental delegation model?
  67. What about the execution process of class loading?
  68. How to judge whether an object can be recycled?
  69. What reference types are there in java?
  70. What garbage collection algorithms does the jvm have?
  71. What garbage collectors does the jvm have?
  72. Introduce CMS garbage collector in detail?
  73. What are the new generation garbage collectors and the old generation garbage collectors? What's the difference?
  74. Briefly describe how the generational garbage collector works?
  75. Let's talk about jvm tuning tools?
  76. What are the commonly used jvm tuning parameters?

When the Java thread executes the native method, the program counter is empty. How to ensure the execution position of the program after the native method is executed
Native is written in non java code, such as C and C + +. They cannot generate bytecode during Java compilation, that is, the JVM cannot obtain the native implementation, and can only call the native method through the system instruction. Therefore, the program counter value is null and undefined when executing native. The native method is directly executed by the native platform. After the native method is executed, it will exit (stack frame pop). The method exits and returns to the called place to continue executing the program.


  • Why is the CPU cache still so small?


Can be understood as socket and fd(file descriptor File descriptor)

10 Medium network ID
 Single thread synchronization -> NTP
 Multithread synchronization -> Natty
 Single threaded asynchronous -> Redis
 Semi synchronous and semi asynchronous -> Natty
 Multi process synchronization -> fastcgi
 Multithreaded asynchronous -> memcached
 Multi process asynchronous -> nginx
 Per request per process (field)-> Apache/CGI
 Micro process framework -> erlang/go/lua
 Protocol framework -> libco/ntyco/libgo

Single thread epoll -> redis
 Multithreading epoll -> memcached
 Multi process epoll -> nginx

epoll The underlying data structure is a red black tree with a ready queue.
while(1) {
    epoll_wait(epfd, events, 1024, -1)
epoll Three functions in
epoll_create() Create a network IO
epoll_ctl() control connection of
epoll_wait() How often do you poll to see if there is data
 There is an event loop on the server side that keeps listening for data (the disadvantage is that it always occupies one) cpu)
  • Explain synchronous / asynchronous, blocking / non blocking
Mom asked me to cook a pot of water in the kitchen and prepare dumplings
 Blocking: as long as the water doesn't boil, I stare at the pot. The sea changes with each passing day. I stand still. The kitchen is my home, and boiling water is my destiny.
Non blocking: I go to my house to beat the king first, but every minute, I have to go to the kitchen to have a look. I'm afraid that if the water is dry for a long time, it will break down, which leads to my mind to play the game. Otherwise, I'll fall out again.
Synchronization: whether it's looking at the pot every minute or watching the pot all the time, as long as I don't go to see it, I don't know whether the water is cooked or not. It's a waste of time. Every inch of time is an inch of gold. This pot must pay me 13 salary
 Asynchronous: I bought an electric kettle on Taobao. As long as the water boils, it makes a sound. Hey, you can beat the king at ease and eat dumplings after beating~
block/Non blocking: what am I doing while I'm waiting for you to work?
Blocking: do nothing, die, etc
 Non blocking: you can do something else, but also ask your progress from time to time
 synchronization/Asynchronous: how can I know when you're done?
Synch: as long as I don't ask, you won't tell me
 Asynchronous: when you're done, just call me over

Let me explain before we discuss IO The objects and steps involved in the occurrence of a network IO,It involves two system objects:
* application Call this IO Process of
* kernel system kernel 

The two interaction processes they experienced are:
 Phase 1 wait for data Waiting for data preparation
 Phase 2 copy data from kernel to user Copy data from kernel to user process

Second kill system

  1. How to control the regular lighting of the purchase button on the second kill product page? [buying problem]
  2. How to prevent oversold?
  3. How to prevent repeated orders?
  4. Queue up to place an order, similar to 12306 entering the queue
    Answer reference:

Interview skills

  1. Before the interview, out of politeness and the interviewer to confirm whether his speech can be understood, the question of dialect
  2. Try to limit self introduction to about 35 seconds (introduction of basic information, work experience and reasons for job hunting)
  3. In answering the question, I use what my understanding is. Don't answer this question absolutely. Technology is not completely right or wrong. Everyone has a different understanding of technology
  4. During the interview, if you encounter a knowledge point you haven't contacted, you can say this: we have contacted this knowledge point, and I'll try to answer my understanding


  1. What do you usually use?
A: if you gather, it's like Collection Lower ArrayList, LinkedList; Set Lower HashSet; Map Lower HashMap,If multithreading and thread safety are involved, it will be used ConcurrentHashMap. 
Q: let's talk first ArrayList and LinkedList Well, in what scenario will they use it?
Answer: ArrayList The underlying data structure of is an array and is not specified ArrayList The initial size is 0 for the first time add When size It will become 10. If the capacity is expanded, it will be 1.5 Times. ArrayList Because the bottom layer is an array, the speed of random search is very fast, and the efficiency of insertion and deletion is relatively low. Because its bottom layer is an array, it is required to be a continuous memory unit when allocating memory space. Therefore, it is not recommended to use it if the amount of data to be stored is large ArrayList. LinkedList The bottom layer of is a two-way linked list with head node and tail node, which provides head insertion( LinkedFirst)Tail insertion( LinkedLast),Insertion and deletion are relatively fast, and random query is not supported, LinkedList The storage of data does not require the memory space to be continuous.
Q: in the case of multithreading, I want to use List,What if you want to ensure thread safety?
A: I know there are three ways: 1. use Vector,It is thread safe List Set, all methods are added synchronized Keyword to ensure synchronization, but its performance is poor.[Both read and write are locked, and the bottom layer is also an array. When expanding, it is twice as much as before] 2. use Collections.SynchronizedList,It is Collections The next static inner class, which puts List All the methods under have become thread safe, and Vector The difference is that it puts synchronized It is added to the code block inside the method to improve the scalability[The granularity of the lock becomes smaller] 3. use CopyOnWriteArrayList,add Lock when reading, and do not lock when reading, which improves the reading performance [The lock granularity becomes smaller and the lock range becomes smaller]
  1. Let's talk about HashMap regularly
Answer: HashMap In 1.7 The underlying array is used when+The data structure of single linked list uses header insertion; one.8 Use array+Single linked list/Red and black trees use tail insertion. The conversion between single linked list and red black tree, when the length of single linked list is greater than or equal to 8, and its hash When the bucket is greater than or equal to 64, it will convert the single linked list into a red black tree for storage; When the number of nodes in the red black tree is less than or equal to 6, it will be converted into a single linked list again, which is a change in its underlying structure; When we go hashmap in put Element, first according to key of hash Value the position of this element in the array (i.e. subscript), and then you can put this element in the corresponding position. If other elements have been stored in the position where this element is located, the elements in the same position will be stored in the form of a linked list. The newly added elements will be placed at the head of the chain and the first elements will be placed at the end of the chain. from hashmap in get Element is calculated first key of hashcode,Find an element at the corresponding position in the array, and then key of equals Method to find the required element in the linked list at the corresponding position. From here we can imagine that if there is only one element in the linked list at each position, then hashmap of get The efficiency will be the highest; Another point is about it Hash The number of barrels is 16 by default, and the default threshold is 0.75,This is related to its expansion.
Q: how is it expanded?
A: during capacity expansion, first check the number of elements in the array. The load factor is by default loadFactor=0.75,Therefore, when the capacity occupied by its hash bucket is greater than 12, it will trigger capacity expansion. The hash bucket will be expanded twice as much as before. The previous elements will be hashed again, then added to the new hash bucket, and then rearranged according to the linked list or red black tree.
Q: can you tell me if it's thread safe?
A: it is not thread safe, because multiple threads may overwrite data during insertion operation; In addition, it is in 1.7 When it was, it was put There may be one when resize The process may be as follows: its head insert will form a circular linked list and form an dead cycle, 1.8 Then it was changed to tail insertion.
Q: how do you ensure its thread safety when developing?
A: I usually use it when developing ConcurrentHashMap To ensure its thread safety. In addition HashTable It can also ensure thread safety, but HashTable The method of locking is.`To be continued`
Q: have a chat ConcurrentHashMap
 Answer: 1.7 The bottom layer is a fragment array, which is used segment Lock (inherited from) ReentrantLock),By locking only one segment at a time to ensure its thread safety and concurrency; In addition, in 1.8 It changed to and HashMap The same data structure, using data plus single linked list or red black tree data structure, in 1.8 Gradually abandoned this slice locking mechanism and used synchronized plus CAS Come on, we know in 1.6 Version of the time JVM right synchronized The optimization of is very large. Now this method is also used to ensure thread safety.
Q: CAS What is it?
Answer: Compare And Sweep,Compare and replace is an implementation of optimistic lock. Before modifying, compare the previously read value with the value in the current memory, and write if it is consistent. CAS It can be considered as a lightweight lock. It is very efficient when concurrency is low. In the case of high concurrency, CAS Need to involve CPU The calculation of is easy to cause CPU Performance consumption. In case of high concurrency, it is recommended to use state machines or locks. Another point is that it will produce ABA Because between reading and writing, other threads may complete the modification first, and then change the value back to the previous one, so they mistakenly think that the current value is consistent with the read value. This problem can be identified by adding a stamp or flag bit, that is, it is equivalent to adding a version number.
  1. You just mentioned synchronized. Talk to me about it again
A: About synchronized,Can be used to synchronize code blocks (any lock can be specified)/Method (specify) this)/Static method (specify) class Object). In 1.6 The upgrade is quite large. First, it provides a lockless state, then a biased lock, then a lightweight lock, and then a heavyweight lock. If the lock is biased, see the meaning of the name. It is biased to the thread that obtains the first lock, and it will id Write to the object header of the lock object. When other threads come, they will immediately upgrade to the state of lightweight lock (if the same thread enters again, first judge whether the lock has been obtained, and if the tag bit has been obtained)+1,Otherwise, it is modified to 1). The lightweight lock is mainly a way to eliminate the lock under the condition of low concurrency. It mainly opens up a space in your virtual machine stack, called Lock Record,Lock the object's Mark Work write to Lock Record,Try again Lock Record Pointer use CAS Modify the area of the lock object header to complete a locking process. If another thread enters and tries to modify the pointer, the lightweight lock will be upgraded to a spin lock. If it fails 10 times, it will expand into a heavyweight lock, that is, a mutually exclusive process. Heavyweight lock, using synchronized You will add two instructions before and after your code block, monitorenter and monitorexist,Through a monitor The monitor monitors the status of this lock by means of a counter. If the synchronization method is used, a ACC_SYNCHRONIZED Flag bit, equivalent to flag,It automatically follows a strategy of synchronous method call. This principle is relatively simple.
Q: when do you use it? When ReentrantLock,Have you considered this?
A: Yes, the difference between the two is quite big. from JVM At the level, synchronized yes JVM A keyword of, ReetrantLock It's actually a class. You need to code it manually, synchronized In fact, the use is relatively simple, and there is no need to care about the release of the lock; But use ReetrantLock You need to go manually when you need to lock,Then cooperate tray finally Then make sure the lock is released, and then ReentrantLock comparison synchronized There are several advanced features. When a thread can't get a lock for a long time, you can manually call one lockInterruptibly Methods try to interrupt without waiting; In addition, it provides a fair lock; In addition, it provides a condition,You can specify to wake up bound to condition Threads on the body to implement a selective notification method. If not required ReentrantLock If you need advanced features, it is recommended to use them synchronized Keywords.
Example: because the lock is irreversible, if all the locks on Didi's taxi are upgraded to heavyweight locks in the morning peak, the lock will still be heavyweight locks after this peak, which will affect the performance of the system QPS Therefore, it should be used with more specific scenes
  1. volatile, have you seen it?
Answer: volatile Modified variables ensure visibility under multithreading, when CPU When writing data, it is found that this variable is volatile When decorating, find other CPU A copy of this variable also exists in the and signals other users CPU The cache line of this variable is set to invalid state, so when other CPU When you need to read this variable and find that the variable line in your cache is invalid, you need to read it in memory again. It is through the bus sniffing mechanism of the computer( MESI)Of course, it will also become a problem, that is volitale It will sniff all the time, resulting in some invalid interactions and bus storm.

Tags: Java Interview linked list

Posted by ramtulasi on Thu, 14 Apr 2022 14:05:56 +0930