Two high frequency design interview questions: how to design HashMap and thread pool

Fan Li 2021-04-08 11:57:02
high frequency design interview questions


I've been collecting interview questions recently , But the version I wrote is not a recitation version , It's not a rote answer .

My original intention is to throw a brick to attract jade , Give my own understanding and explanatory answers to each question , Then reciting the version requires you to summarize and memorize by yourself .

Because eight part essay is a must in an interview , That is to say, we still need to know what we should know , And on the basis of understanding, memory will be more profound , And can deal with some variant problems .

But it's not clear if this form is popular , So for the time being, I'll take two topics and send them out first to see the repercussions .

So if you think this kind of form is not good , If it needs to be improved , Please leave a message .

1. If you were to design a HashMap How to design ?

I think this problem can be solved from HashMap Some of the key points to start with , for example hash function 、 How to deal with conflicts 、 How to expand .

Let's say you're right HashMap The understanding of the .

such as :HashMap It's just a storage <key,value> Set of formats , Made by key stay O(1) You can find it in a complicated time value.

The basic principle is to key after hash Hash function to get the hash value , Then the hash value is used to modulate the array to get the corresponding index .

therefore hash Functions are key , It's not just fast , It also needs to be evenly distributed , Reduce hash Collision .

And because the input value is infinite , And the size of the array is limited, so there must be collisions , So we can use zipper method to deal with conflicts .

To avoid malicious hash attack , When the zipper exceeds a certain length, it can turn into a red black tree structure .

Of course, more than a certain number of nodes still need to be expanded , Otherwise, the collision would be too serious .

The common expansion will lead to a certain failure put The delay is large , especially HashMap When there is a lot of data stored , So we can consider and redis Two like that table Delay movement , You can move only part of it at a time .

But this memory is tight , So it's also the scene trade off 了 .

also , It's best to estimate the data size before using it , Avoid frequent expansion .

Basically, this is the answer ,HashMap There are several key elements , Next, it depends on how the interviewer asks .

It may extend to issues like thread safety , Anyway, just follow currentHashMap My design answers .

In fact, some questions seem to ask how to design , In fact, you just answer how you understand this thing , Let's talk about its principle and some key points. This topic is over .

For example, the size of the estimated data I mentioned above , It doesn't seem to have anything to do with design , But it's enough to let the interviewer know that you are sensitive to this aspect .

So sometimes “ give an irrelevant answer ” yes OK Of , If the interviewer thinks you're going in the wrong direction , Naturally, it will remind you , You'll take the call then .

In short , A lot of questions don't really need to be answered rigidly to the interview questions , Because the interviewer just asked in general .

2. If you want to design a thread pool, how to design it ?

This kind of design problem is still the same , Let's talk about understanding first , Show that you know the use and principle of this thing , And then start BB. Basically, according to the existing design , Add some personal insights .

The thread pool is a container for storing threads , Save the previously established thread in the pool to repeat the task , Reduce the overhead of creating and destroying threads , Improve the response speed of the task , And convenient for thread management .

I personally think that if we want to design a thread pool, we have to consider the management of working threads in the pool 、 Task choreography execution 、 Thread pool overload solution 、 Monitoring, etc .

To initialize the number of threads 、 Number of core threads 、 Maximum thread pools are exposed and configurable , Including the configuration of thread idle death exceeding the number of core threads .

Then the storage structure of the task has to be configurable , It can be unbounded or bounded , Or according to the configuration , Multiple queues are used to assign tasks with different priorities , You can also use stealing To improve the utilization of threads .

Then provide configuration to indicate that the thread pool is IO Intensive or CPU Intensive to change the execution strategy of the task .

There are many options for overload , Including dropping tasks 、 Reject the task and throw an exception 、 Discard the oldest tasks or customizations, etc .

As for monitoring , The design of thread pool should be better , Expose the interface for monitoring , Such as the number of tasks processed 、 Number of tasks to be processed 、 Number of running threads 、 The number of rejected tasks and so on .

I think that's basically the answer , Just wait for the interviewer to ask .

Note that you don't need to explain to the interviewer what the number of core threads is , You know, it's not necessary .

Of course, different people have different opinions on this kind of open-ended problem , This is not the standard answer , For reference only .

All the related keywords of the thread pool should be mentioned , On the surface, your understanding of the internal principles of thread pooling is thorough .

This article is from WeChat official account. - High performance server development (easyserverdev)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2021-04-02

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Fan Li]所创,转载请带上原文链接,感谢
https://javamana.com/2021/04/20210408111750813v.html

  1. A love diary about http
  2. navicat连接win10 mysql8.0 报错2059
  3. [rocketmq source code analysis] in depth message storage (3)
  4. Implementation of service configuration center with spring cloud + Nacos (Hoxton version)
  5. SCIP: constructing data abstraction -- Explanation of queue and tree in data structure
  6. SCIP: abstraction of construction process -- object oriented explanation
  7. Using docker to build elasticsearch + kibana cluster
  8. What are the spring IOC features? I can't understand the source code!
  9. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  10. 导致Oracle性能抖动的参数提醒
  11. 风险提醒之Oracle RAC高可用失效
  12. 小机上运行Oracle需要注意的进程调度bug
  13. Oracle内存过度消耗风险提醒
  14. Oracle SQL monitor
  15. 使用Bifrost实现Mysql的数据同步
  16. 揭秘Oracle数据库truncate原理
  17. 看了此文,Oracle SQL优化文章不必再看!
  18. Mybatis (3) map and fuzzy query expansion
  19. Kafka性能篇:为何这么“快”?
  20. 两个高频设计类面试题:如何设计HashMap和线程池
  21. [TTS] AIX - & gt; Linux -- Based on RMAN (real environment)
  22. 为什么学编程大部分人选Java编程语言?
  23. Redis 高可用篇:你管这叫 Sentinel 哨兵集群原理
  24. redis 为什么把简单的字符串设计成 SDS?
  25. [TTS] transfer table space AIX - & gt; Linux based on RMAN
  26. Linux 网卡数据收发过程分析
  27. Redis 高可用篇:你管这叫 Sentinel 哨兵集群原
  28. Redis 6.X Cluster 集群搭建
  29. [TTS] transfer table space AIX ASM - & gt; Linux ASM
  30. [TTS] transfer table space Linux ASM - & gt; AIX ASM
  31. 高性能通讯框架——Netty
  32. Brief introduction and test of orchestrator, a high availability management tool for MySQL
  33. [TTS] transfer table space Linux - & gt; AIX based on RMAN
  34. A love diary about http
  35. [rocketmq source code analysis] in depth message storage (3)
  36. Implementation of service configuration center with spring cloud + Nacos (Hoxton version)
  37. SiCp: abstraction of construction process -- object oriented explanation
  38. springboot网上点餐系统
  39. 【SPM】oracle如何固定执行计划
  40. 用好HugePage,告别Linux性能故障
  41. 3 W word long text, java basic interview questions! It's amazing!!!
  42. Spring cloud upgrade road - 2020.0. X - 3. Accesslog configuration of undertow
  43. Win10 uninstall mysql5.7
  44. CentOS下dotnet Core使用HttpWebRequest进行HTTP通讯,系统存在大量CLOSE_WAIT连接问题的分析,已解决。
  45. MySQL batch insert, how not to insert duplicate data?
  46. K8s cronjob application example
  47. Unconventional method, easy to deal with Oracle database critical exception
  48. How to use sqlplus - prelim in Oracle hang
  49. How to search Oracle official documents in full text
  50. Install mysql8.0 on win10
  51. Oracle OCR的备份与恢复
  52. Oracle kill session相关问题
  53. 《Oracle DBA工作笔记》第二章 常用工具和问题分析
  54. Oracle回收站及flashback drop
  55. Hand in hand to teach you to write a spring IOC container
  56. Exception in Java (1) - basic concept
  57. 3w 字长文爆肝 Java 基础面试题!太顶了!!!
  58. Error 2059 when Navicat connects to win10 mysql8.0
  59. Parameter reminder causing Oracle Performance jitter
  60. 「技术分享」Java线程状态间的互相转换看这个就行了