Common questions about java interview! A must-see when preparing for the spring recruitment interview!

table of Contents

Interview questions

Java basics

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

Multithreading

  1. What is the difference between parallel and concurrency?
  2. The difference between thread and process?
  3. What is a daemon thread?
  4. What are the ways to create threads?
  5. What is the difference between runnable and callable?
  6. What are the statuses of threads?
  7. What is the difference between sleep() and wait()?
  8. What is the difference between notify() and notifyAll()?
  9. What are the ways to create a thread pool?
  10. What is the difference between thread run() and start()?
  11. What are the statuses of the thread pool?
  12. What is the difference between the submit() and execute() methods in the thread pool?
  13. How to ensure the safe operation of multiple threads in a Java program?
  14. What is the upgrade principle of multithreaded locks?
  15. What is a 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 is the difference between synchronized and Lock?
  21. What is the difference between synchronized and ReentrantLock?
  22. Tell me about the principle of atomic?

spring/spring MVC

  1. Why use spring?
  2. Explain what is aop?
  3. Explain what is ioc?
  4. What are the main modules of spring?
  5. What are the common injection methods for spring?
  6. Are beans in spring thread-safe?
  7. What kind of scope does spring support for beans?
  8. What are the ways to automatically assemble beans in spring?
  9. What are the spring transaction implementation methods?
  10. Talk about spring's transaction isolation?
  11. Talk about the running process of spring mvc?
  12. What are the components of spring mvc?
  13. What is the purpose of @RequestMapping?
  14. What is the role of @Autowired?
    Four, spring/spring Cloud
  15. What is spring boot?
  16. Why use spring boot?
  17. What is the spring boot core configuration file?
  18. What types of spring boot configuration files are there? What is the difference between them?
  19. What are the ways to achieve hot deployment with spring boot?
  20. What is 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?
    Five, Mybatis
  24. What is the difference between #{} and ${} in mybatis?
  25. How many paging methods does mybatis have?
  26. Is RowBounds querying all results at once? why?
  27. What is the difference between mybatis logical paging and physical paging?
  28. Does mybatis support lazy loading? What is the principle of lazy loading?
  29. Tell me about the first level cache and the second level cache of mybatis?
  30. What are the differences between mybatis and hibernate?
  31. What executors does mybatis have?
  32. What is the realization principle of mybatis paging plug-in?
  33. How to write a custom plug-in for mybatis?
    Six, kafk, Zookeeper
  34. Can Kafka be used separately from zookeeper? why?
  35. How many data retention strategies does Kafka have?
  36. Kafka has set up 7 days and 10G to clear data at the same time. By the fifth day, the message reached 10G. What will Kafka do at this time?
  37. What can cause Kafka to run slower?
  38. What should I 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 synchronization of the master and slave nodes?
  43. Why is there a master node in the cluster?
  44. There are 3 servers in the cluster, and one of the nodes is down. Can zookeeper still be used at this time?
  45. Tell me about the notification mechanism of zookeeper?
    Seven, 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 to deal with it?
  52. What data types does redis support?
  53. What are the Java clients supported by redis?
  54. What is the difference between Jedis and redisson?
  55. How to ensure the consistency of cache and database data?
  56. How many ways are redis persistence?
  57. How does redis implement distributed locks?
  58. What are the shortcomings of redis distributed locks?
  59. How does redis optimize memory?
  60. What are the redis elimination strategies?
  61. What are the common performance problems of redis? How to solve it?
    Eight, JVM
  62. Tell me about the main components of Jvm? And its role?
  63. Talk about the Jvm runtime data area?
  64. Tell me about the difference between stacks?
  65. What are queues and stacks? What's the difference?
  66. What is the parent delegation model?
  67. Talk about the execution process of class loading?
  68. How to judge whether the object can be recycled?
  69. What are the reference types in java?
  70. Tell me about what garbage collection algorithms does jvm have?
  71. Tell me about what garbage collectors jvm has?
  72. Tell me more about the CMS garbage collector?
  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. 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 that the location of the program execution after the native execution
is written by non-java code, such as C, C++, they cannot generate bytecode during java compilation, that is, the JVM cannot get it In native implementation, native methods can only be called through system instructions, so the program counter value is empty and undefined when native is executed. The native method is directly executed by the native platform. After the native method is executed, it will exit (stack frame pop), and the method exits and returns to the place where it was called to continue executing the program.

computer

  • Why is the CPU cache still so small?

database

可以理解为socket和fd(file descriptor文件描述符)

10中网络ID
单线程同步 -> NTP
多线程同步 -> Natty
单线程异步 -> Redis
半同步半异步 -> Natty
多进程同步 -> fastcgi
多线程异步 -> memcached
多进程异步 -> nginx
每请求每进程(现场)-> Apache/CGI
微进程框架 -> erlang/go/lua
协议框架 -> libco/ntyco/libgo

单线程 epoll -> redis
多线程 epoll -> memcached
多进程 epoll -> nginx

epoll底层数据结构是红黑树,还有一个就绪队列。
while(1) {
    epoll_wait(epfd, events, 1024, -1)
}
epoll中的三个函数
epoll_create() 创建一个网络IO
epoll_ctl() 控制connection的
epoll_wait() 多长时间去轮询一次,看有没有数据
服务器端有个事件循环一直在监听有没有数据(缺点是一直占一个cpu)
  • Explain synchronous/asynchronous, blocking/non-blocking
妈妈让我去厨房烧一锅水,准备下饺子
阻塞:水只要没烧开,我就干瞪眼看着这个锅,沧海桑田,日新月异,我自岿然不动,厨房就是我的家,烧水是我的宿命。
非阻塞:我先去我屋子里打把王者,但是每过一分钟,我都要去厨房瞅一眼,生怕时间长了,水烧干了就坏了,这样导致我游戏也心思打,果不然,又掉段了。
同步:不管是每分钟过来看一眼锅,还是寸步不离的一直看着锅,只要我不去看,我就不知道水烧好没有,浪费时间啊,一寸光阴一寸金,这锅必须发我13薪
异步:我在淘宝买了一个电水壶,只要水开了,它就发出响声,嗨呀,可以安心打王者喽,打完可以吃饺子喽~
总结:
阻塞/非阻塞:我在等你干活的时候我在干啥?
阻塞:啥也不干,死等
非阻塞:可以干别的,但也要时不时问问你的进度
同步/异步:你干完了,怎么让我知道呢?
同步:我只要不问,你就不告诉我
异步:你干完了,直接喊我过来就行

在讨论之前先说明一下IO发生时涉及到的对象和步骤,对于一个network IO,它会涉及到两个系统对象:
* application 调用这个IO的进程
* kernel 系统内核

那他们经历的两个交互过程是:
 阶段1 wait for data 等待数据准备
 阶段2 copy data from kernel to user 将数据从内核拷贝到用户进程中

Spike system

  1. How to control the timing of the purchase button on the spike product page? [Shopping problem]
  2. How to prevent oversold?
  3. How to prevent repeated orders?
  4. Queue to place an order, similar to the operation of 12306 entering the Queue.
    Reference answer: https://github.com/zhonghuasheng/JAVA/tree/master/seckill#

Interview skills

  1. Before the interview, out of politeness and the interviewer to confirm whether one's speech can be understood, dialect problems
  2. Try to keep your self-introduction within 35 seconds (basic information introduction, work experience introduction, job search reason introduction)
  3. When answering the question, I use what I understand. Don’t answer this question absolutely. There is no right or wrong technology. Everyone has a different understanding of technology.
  4. If you encounter a knowledge point that you have not touched during the interview, you can say this: We have contacted this knowledge point, I will try to answer my understanding

Pannier

  1. Which collections do you use more often?
答:集合的话像Collection下的ArrayList, LinkedList; Set下的HashSet; Map下的HashMap,如果涉及到多线程,线程安全的话,会使用ConcurrentHashMap。
问:我们先聊一下ArrayList和LinkedList吧,它们两个在什么场景下会使用它?
答:ArrayList的底层数据结构是数组,不指定ArrayList大小的时候初始化的大小是0,第一次add的时候size会变成10,扩容的话会是之前的1.5倍。ArrayList由于底层是数组,因此随机查找的速度很快,插入和删除效率比较低。因为它底层是数组,因此分配内存空间的时候要求是连续的内存单元,所以如果需要存储的数据量很大的情况下,不建议使用ArrayList。LinkedList的底层是一个带有头节点和尾节点的双向链表,提供了头插(LinkedFirst)和尾插(LinkedLast),插入和删除比较快,不支持随机查询,LinkedList数据的存储不要求内存空间是连续的。
问:如果在多线程的情况下,我既想用List,又想保证线程安全,那怎么办?
答:我知道的有三种方式:1. 使用Vector,它是一个线程安全的List集合,所有的方法都加了synchronized关键字来保证同步,但它性能很差。[读写都加锁,底层也是数组,扩容时是之前的2倍] 2. 使用Collections.SynchronizedList,它是Collections下的一个静态内部类,它把List下的所有方法都变成了线程安全的,于Vector不同的是,它把synchronized加到了方法内部的代码块上,提高了扩展性[锁的粒度变小] 3. 使用CopyOnWriteArrayList,add的时候加锁,读的时候不加锁,提高了读取性能 [锁粒度变小,同时锁范围变小]
  1. Let's talk about HashMap regularly
答:HashMap在1.7的时候底层使用数组+单链表的数据结构,使用的是头插;1.8使用数组+单链表/红黑树,使用的是尾插。单链表和红黑树之间的转换,当单链表的长度大于等于8,并且它的hash桶大于等于64的时候,它会将单链表转换成红黑树形式存储;它在红黑树的节点的数量小于等于6的时候,它会重新再转换成一个单链表,这是它底层结构的一个变化;当我们往hashmap中put元素的时候,先根据key的hash值得到这个元素在数组中的位置(即下标),然后就可以把这个元素放到对应的位置中了。如果这个元素所在的位子上已经存放有其他元素了,那么在同一个位子上的元素将以链表的形式存放,新加入的放在链头,最先加入的放在链尾。从hashmap中get元素时,首先计算key的hashcode,找到数组中对应位置的某一元素,然后通过key的equals方法在对应位置的链表中找到需要的元素。从这里我们可以想象得到,如果每个位置上的链表只有一个元素,那么hashmap的get效率将是最高的;另外一点是关于它Hash桶的数量,默认是16个,默认的阀值是0.75,这关系到它的扩容。
问:它扩容是怎么扩容的?
答:扩容的时候首先检测数组中的元素个数,负载因子默认是loadFactor=0.75,因此当它哈希桶占用的容量大于12的时候,就会触发扩容,哈希桶会扩容成之前的两倍,把之前的元素再进行一次哈希运算,然后添加到新的哈希桶,然后按照链表或者红黑树重新排列起来。
问:你给我说下它是不是线程安全的?
答:它不是线程安全的,因为在插入操作的时候多线程会有数据覆盖的可能;另外它在1.7的时候,它在put的时候可能会有一个resize的过程,这个过程可能会照成它的头插会形成一个环形链表形成一个死循环,1.8之后改成尾插了。
问:你平时开发的时候怎么去保证它的线程安全?
答:我平时开发的时候会使用ConcurrentHashMap来保证它的线程安全。另外有HashTable也能保证线程安全,但是HashTable锁的是方法。`待续`
问:聊一聊ConcurrentHashMap
答:1.7的时候底层是一个分片数组,使用了segment锁(继承自ReentrantLock),通过每次只给一个段加锁来保证它的线程安全和并发度;另外,在1.8的时候,它改成了和HashMap一样的数据结构,使用数据加单链表或者红黑树的数据结构,在1.8的时候渐渐放弃了这种分片锁机制,而使用的是synchronized加CAS来做的,我们知道在1.6版本的时候JVM对synchronized的优化是非常大的,现在也是用这个方法来保证线程安全。
问:CAS是什么?
答:Compare And Sweep,比较并替换,是乐观锁的一种实现,在修改之前要将之前读到的值与当前内存中的值进行比较,如果一致就写入。CAS可以认为是一种轻量级的锁,在低并发的时候时候效率很快,在高并发的情况下,CAS需要涉及到CPU的计算,容易引起CPU性能的消耗,高并发的情况下还是建议使用状态机或者锁之类的。另外一点就是会产生ABA的问题,因为在读取和写入之间,可能有其他线程率先完成修改,然后又将值改回到之前,这样就误以为当前的值和读取的值是一致的,这个问题可以通过加一个戳或标志位来标识,也就是相当于加个版本号。
  1. You mentioned synchronized just now, then talk to me about it
答:关于synchronized,可以用在同步代码块(可指定任意锁)/方法(指定this)/静态方法(指定class对象)。在1.6升级还是蛮大的,首先提供了无锁状态,然后是偏向锁,然后是轻量级锁,然后是重量级锁。偏向锁的话,见名知意,它偏向于获得第一个锁的线程,它会将id写到锁对象的对象头中,等其他线程来的时候,立即会升级到轻量级锁的状态(如果是同一个线程再次进入,先判断是否获取过锁,如果获取过标记位+1,否则修改为1),轻量级锁主要是在低并发的情况下来消除锁的方式,它主要是在你的虚拟机栈中开辟一个空间,叫做Lock Record,将锁对象的Mark Work写到Lock Record,再尝试Lock Record的指针使用CAS去修改锁对象头的那个区域来完成一个加锁的过程,如果再有线程进入的话,并尝试修改这个指针,轻量级锁会升级为一个自旋锁,如果10次未成功就会膨胀成一个重量级锁,也就是一个互斥锁的过程。重量级锁,使用synchronized的时候会在你的代码块前后加上两个指令,monitorenter和monitorexist,通过一个monitor监视器通过计数器的方式来监视这个锁的状态。如果是同步方法的时候,使用的是一个ACC_SYNCHRONIZED标志位,相当于flag,它自动走的是一个同步方法调用的策略,这个原理是比较简单的。
问:什么时候用它?什么时候用ReentrantLock,这个你有考虑吗?
答:有,它两对比的话区别还是蛮大的。从JVM层面来说,synchronized是JVM的一个关键字,ReetrantLock其实是一个类,你需要手动去编码,synchronized使用其实是比较简单的,不需要关心锁的释放;但是使用ReetrantLock的时候你需要手动去lock,然后配合tray finally然后去确保锁的释放,然后ReentrantLock相比synchronized有几个高级特性,当一个线程长期等待得不到锁的时候,你可以手动的去调用一个lockInterruptibly方法尝试中断掉不去等待;另外,它提供了一个公平锁的方式;此外,它提供了一个condition,你可以指定去唤醒绑定到condition身上的线程,来实现一个选择性通知的方式。如果不需要ReentrantLock需要的高级特性的话,建议还是使用synchronized的关键字。
例:因为锁不可逆,如果在早高峰的时候,滴滴打车上的所有锁都升级为重量级锁,那么等过了这个高峰,锁依然是重量级锁,会影响系统的QPS的,所以在使用的时候还是要更具具体的场景来使用
  1. Have you seen volatile?
答:volatile修饰的变量保证了多线程下的可见性,当CPU写数据时,发现此变量被volatile修饰时,发现其他CPU中也存在该变量的副本,会发出信号通知其他CPU该变量的缓存行置为无效状态,因此当其他CPU需要读取这个变量时,发现自己缓存中的变量行是无效的,就重新去内存读取。它是通过计算机的总线嗅探机制(MESI)来实现的,当然它也会照成一个问题,就是volitale会一直嗅探,导致一些无效的交互,引发总线风暴。

Guess you like

Origin blog.csdn.net/weixin_43314519/article/details/114238395