2020 interview questions are freshly released-continuous update

More good text, please pay attention to micro-channel public number Java book club oh

Write in front

The so-called gold three silver four, my book friends and friends recently shared a lot of interview questions, specially summarized and shared a wave. Before we start, I need to talk nonsense. In our daily work, most of us may still focus on business, but in the eyes of the interviewer, you must master the principles of all the technology stacks involved in your project. This is not easy for you who are in contact with the corners. Outside of work, you may have learned a lot of technology stacks, but most of the time, they are only limited to theory, lacking actual combat scenarios, and may forget them after a while. The reason is that the current knowledge system of the Java ecosystem is too large. If you want to cover everything, you can't do it in one day. We must sink our hearts to one knowledge point and one knowledge point to conquer. The following interview questions will be continuously updated to achieve full coverage. I try to answer these interview questions in the form of a vernacular dialogue that I understand. But this does not mean that you can sit back and relax in the interview after you have memorized these questions, but to sort out your knowledge tree through these questions. In accordance with your own language and way of thinking, internalizing these knowledge points into your own things is your ultimate goal. Okay, not much nonsense, the text begins.

Start of text

1. How to implement custom annotations?

嗯,我拿工作当中的一个例子来说明吧,就是我们工作中会有一些系统日志的场景,就是自定义注解来实现的。
我们自定义这个注解的话,用@interface来声明一个类Syslog,同时加上元注解@Retention(RetentionPolicy.RUNTIME), 用来标明自定义的注解可以保留到运行期。此外还要加上@Target(ElementType.METHOD)元注解,标明改自定义注解是被使用方法上。这样的话,便实现了一个自定义注解。除此之外,我们也可以引入@Inherited元注解,这样定义的注解是可以被继承的,就是说一个父类给这样自定义的注解修饰的话,那么他的子类也默认被该注解修饰。

2. Customize how you use it, or how does it take effect when you use it?

还是拿刚才说的那个自定义系统日志来说明吧。我们是通过Spring AOP切面织入的方式去是这个自定义注解生效的。通过Spring 自身的@Aspect

3. How does spring create beans?

嗯,Spring创建Bean一般有三种方式,分别是利用构造器创建,静态工厂创建,实例工厂方法创建,都需要配置相应的xml文件。交给Spring去解析创建。

4. What is the life cycle of a spring bean?
The Spring life cycle is mainly divided into four stages: initial property assignment, instantiation and destruction. The specific process is that Spring will load the configuration file when the project starts, and encapsulate the class information in the file as BeanDefination. The core step is to assign a value to the class name and create an instance through reflection, and then add it to the IOC container. The destruction is to remove the beanWapper object from the container.
5. What are the classes under the juc package?

juc:java Util Concurrent包,主要有原子类、锁、还有队列、线程池、并发集合等。

6. What are the core parameters of the thread pool?

线程池的核心参数有:核心线程数、最大线程数、存活时间、队列、拒绝策略。

7. The role of thread pool queues?

主要目的在于,等待核心线程数执行完,提高线程的可复用性。刚才我们提到了线程池的几个核心参数。当用户线程进入线程池后,会先查看核心线程数有没有用完,如果没有,则创建线程;如果有,则加入的阻塞队列。持续不断的线程进来,会继续看阻塞队列是否已满,未满 则会继续创建线程进行执行。如果已满,则会去执查看最大线程数是否有剩余,有的话,继续创建线程进行执行。如有没有,则去执行拒绝策略。

8. How do you use thread pools in your work?

在日常开发项目中,我们有大量的业务场景使用到了线程池。例如:图书终审,图书听书定时上架,之前很早做的集团用户导入等业务场景。针对项目中大量是用的线程池场景,我们有封装了一个线程池管理类,来保证不同的线程池是单例的,并且给了兜底的线程池配置。不同的任务也可以自己配置相应的参数。然后创建不同的线程任务,扔给这个线程池管理去执行execute()方法。

9. What is the difference between the execute() method and the submit() method of the thread pool?

我们都知道,线程池的execute()方法是开启线程池中的任务。线程池的submit()方法同样可以做到,但是submit()方法执行相应的任务之后可以返回执行结果Future对象。如此一来,submit()方法可以判断线程池中的任务什么时候完成或者是捕获线程池执行中的异常。通常使用在我们需要知道线程池执行结果的场景。看过线程池这两个方法的源码,我们可以发现,submit()方式是对execute()方法的一个封装,将传入的任务通过RunnableFuture对象进行了封装,实际上还是调用了execute()方法。

Talk about your understanding of nio? Talk about your understanding of docker? Common commands for docker? What is the role of k8s? Class loading mechanism? What are the ways to implement distributed locks? How does zk implement distributed locks? How to check whether the mysql index is effective? The default port of redis? The realization of redis hash slot? What problems did you encounter at work and how did you solve them?

Talk about the implementation principle of RPC framework dubbo?

Talk about sql optimization?

随着项目业务数据量增大的情况,sql的执行效率则会影响程序的运行。那么怎么优化呢?
1、对于查询语句来说,要尽可能避免全面扫描,对于模糊查询的字段和排序的字段优先建立索引。
2、避免where子句使用null进行判断,否则会走全表扫描,可以给予默认值。
3、避免where子句中使用不等于 (!=或者<>)操作符,如此操作也会引起全表扫描。
4、避免where子句使用or进行查询,可以使用union all 进行代替。
5、慎用in或者not in,也会导致全表扫描,连续数据可以采用 between and
6、禁用全模糊查询,而采用右模糊查询,如此可以走索引。而左模糊是不能走索引的。至于为什么呢?因为我们的索引是一棵B+树,从左到右查询是有序的。所以只有在左侧是定值的时候,才能去确定索引。下面这种情况,却会是走到索引的。
例如where子句: where a = '1' and  b like '%2' (a,b)建立联合索引。这种情况就属于索引下推了。
7、避免where子句进行表达式操作(例如加减乘除),该操作会导致数据库放弃索引。
8、避免where子句进行函数操作,该操作会引起全表扫描。
9、使用复合索引时,应该满足最左优先原则,使得用到左边的字段,并且尽可能保证索引字段和查询条件顺序一致。
10、使用exists 代替in进行查询。例如:
select num from a where num in(select num from b)    
用下面的语句替换:    
select num from a where exists(select 1 from b where num=a.num)  
11、对于有大量重复数据的字段来说,建立了索引也无法生效。如果非要这么做,建议使用该字段和其他字段作为组合索引。
12、索引并不是越多越好,在提升出了查询效率的同时,会降低insert及update效率,因为在执行新增或者更新的时候有可能会重建索引。同时,一张表的索引最多不要超过6个,太多的话,可以去掉一些不常用的字段索引。

The difference between cache breakdown and cache avalanche?

在这里插入代码片

How to avoid cache avalanches?

How to avoid repeated consumption of messages?

How to solve the problem of message accumulation?

How to solve the problem of message loss?

How to achieve redis double write consistency?

Distributed lock?

Talk about the memory model of JVM?

After an object is new, what is its process in memory?

Tell me about your understanding of CAP theory?

嗯,CAP理论是指在分布式系统当中,一致性(C-Consistency),可用性(A-Availability),分区容错性(Partition torlerance),这三者最多只能同时满足两点,不可能是三者同时满足。

Talk about the difference between AOP and AspectJ?

AspectJ和Spring AOP都是对目标类增强,生成代理类。
AspectJ是在编译期间将切面代码编译到目标代码的,属于静态代理;Spring AOP是在运行期间通过代理生成目标类,属于动态代理。
AspectJ是静态代理,故而能够切入final修饰的类,abstract修饰的类;Spring AOP是动态代理,其实现原理是通过CGLIB生成一个继承了目标类(委托类)的代理类,因此,final修饰的类不能被代理,同样staticfinal修饰的方法也不会代理,因为staticfinal方法是不能被覆盖的。在CGLIB底层,其实是借助了ASM这个非常强大的Java字节码生成框架。关于CGLB和ASM的讨论将会新开一个篇幅探讨。
Spring AOP支持注解,在使用@Aspect注解创建和配置切面时将更加方便。而使用AspectJ,需要通过.aj文件来创建切面,并且需要使用ajc(Aspect编译器)来编译代码;

Spring AOP我们可以使用Aspectj提供的注解;换句话说就是使用Aspectj的语法风格,所以你在做springAop的时候@Aspect@Before@PonitCut等等这些注解其实都是Aspectj提供的,不是spring提供的

Guess you like

Origin blog.csdn.net/aiwaston/article/details/105064345