When I went to Jingdong in Java 2 years ago, I died, what is the difference between me and the programmers of big factories?

Foreword:

The high salaries and benefits of Internet companies such as BATJ have attracted many engineers to join, and the difficulty of interviews has also increased. It was not yesterday that a classmate asked me and said that when I went to JD for an interview last week, I didn't get past the interview and was very shocked. After careful conversation, I feel that he still has insufficient technical reserves and does not have a deep understanding of some knowledge, so he said that he has not lost money this time, and his experience is also very good. According to his interview, I will sort out the questions and answers. I hope that I can help you in Jinjiuyinshi.

Insert picture description here

TreeSet/HashSet difference

As the name suggests, the first is the difference
in structure 1. The structure behind TreeSet is TreeMap, which is red-black tree, which can realize automatic sorting. It compares content through the equals method or the compareTo method.
2. Behind the HashSet is the HashMap, and the keys are unordered and can only be sorted externally. Since it is a Hash, then it is necessary to rewrite the hashCode and equals methods of the object.
In addition, there is a subtle difference that can be used to install b:
1. HashSet can accept null values, and there is only one.
2. TreeSet cannot accept null by default. Value, it will directly throw a null pointer exception. There is no duplicate data in the set, and there is no nothing in the TreeSet.

In addition, I have compiled and collected more than 20 years of company interview knowledge points, as well as various Java core knowledge points for free to share with you. The following are just some screenshots. If you want information, you can also click 795983544 to receive the secret code CSDN.

Insert picture description here

How does HashMap resolve conflicts and expand its capacity

Questions on the lousy street, answer any questions. Such a thing is the backrest.
The internal structure of HashMap is actually an array + linked list (after java8, if the length is greater than 8, it will be converted to a red-black tree). When HashMap is initialized, there are 16 hash slots by default.
When saving an object, first, locate the hash slot through the hashCode of the object. If multiple objects fall into the same slot at the same time, the linked list will be used to resolve the conflict on this slot.
When HashMap is created, there will be a load factor. Each put operation will check whether the current capacity will exceed the threshold (initailCapacity*loadFactor). If it exceeds, the expansion will be twice the current capacity. After the expansion, the data needs to be hashed again, which is the transfer method.

Experience: resizing is very time-consuming, so if the capacity can be estimated in advance, initailCapacity can be fixed in advance.
Insert picture description here

How does ConcurrentHashMap achieve high concurrency

Simply put, a segmented lock (detached lock) is used. Each lock is used to lock a part of the data in the container, reducing competition for locks between threads.
This question will be deadly when asked in the depths. The space is limited and not long-winded.

How the thread pool is usually used

Ordinary scenes can be created using the factory class Executors. There are three commonly used: Single, Fixed, and Cached.
More often, for finer control, the ThreadPoolExecutor class will be directly customized. Ali's specifications also require this (of course, a lick), and I am especially concerned about the blocking queue and saturation strategy.
Of course, you can only say that if you are familiar with blocking queues and rejection strategies. Otherwise, it would be too clever to dig a hole for yourself. They like you to mention the Ali standard, which makes me think that the design of jdk is very low.

How many ways are there for multiple threads to wait to a certain node and then release them uniformly?

The most classic is CountDownLatch, the main thread is blocked in the await method, and each thread calls countDown. Can solve some classic horse racing problems.
There is also a variant called CyclicBarrier. Each thread is blocked in the await method and is released collectively when a certain threshold is reached. You can also use some of the more elementary APIs, such as the join method of Thread. Future's get method, etc. Complex is not recommended. You can also answer sleep. is there a problem? I can use while to wait for a variable, but why should I do this?

Database index structure

B+ Tree, an index structure born to adapt to slow disks. It must be ensured to query according to the leftmost prefix of the index.
Hash is similar to HashMap. The way to deal with conflicts is that
the index structure of the linked list pg is added. Why does it feel weird with so little Mysql? Should I answer the difference between storage engines?

select * from t where a=? and b>? order by c limit 0,100 How to add index

Knowing this is the conclusion => When the order by field appears in the where condition, the index will be used without sorting. In other cases, order by will not be sorted.
According to the leftmost principle, I can create an index of (a, b).
What is a clustered index and a non-clustered index
A table can only have one clustered index. The main index file and the data file are the same file, the default InnoDB supports clustered index, and the data on the leaf node of the B+ Tree is the data itself. MyISAM does not support clustered indexes. Its leaf nodes store not the data itself, but the address where the data is stored. In the file structure, it will be divided into an index file and a data file. It is useless for programming.

Explain optimistic locking and pessimistic locking

Pessimistic locking always assumes the worst situation. Every time you manipulate data, you think that others will modify it, so you can lock it to ensure safety. Visitors behind can only wait. Row locks, table locks in the database, synchronization keywords in java, etc. are all pessimistic locks.
Optimistic locking is just the opposite. It always assumes the best situation, without locking the data, but with an extra judgment operation. For example, a large number of CAS operations in the concurrent package, a mechanism to determine the new and old version numbers, etc. The pessimistic lock is a wife, and you are the only one; the optimistic lock is a friend, plan by appointment...

How to implement dynamic proxy? What is the difference between CgLib and jdk proxy?

Java implements the InvocationHandler interface to implement dynamic proxy, and then uses Proxy to initialize it.
Cglib uses ASM itself to generate a framework that can proxy ordinary classes, but not final classes, and jdk can only proxy interfaces. In spring, cglib wins.

What are the mainstream implementations of distributed locks? What is the difference between redis and zk locks?

It is roughly divided into two categories.
Optimistic lock: Based on the version number mechanism and CAS implementation, it has nothing to do with the storage of the version number.
Pessimistic lock:

  1. Based on database records, write data when entering and delete records when exiting
  2. Database row lock, such as distributed quartz, it is an exclusive lock
  3. Setnx function based on Redis (since most will set timeout, it is recommended to use set atomic function with px)
  4. Based on zookeeper

Difference:
Redis acquires a lock by a round-robin mechanism. After the lock is released, multiple callers will compete, and some tasks may starve to death.
zk is a monitoring mechanism and will be notified if there is a change. In addition to unfair locks, fair locks can also be implemented.
In terms of elegance, redis obviously wins

What is the function of ThreadLocal? Let's talk about the usage

ThreadLocal is used to isolate data.
ThreadLocal stores thread-related data, and the bottom layer is actually a map. The map for storing data can be obtained through threads.
This method is similar to Request in Servlet. Some data that needs to be bound to threads, such as some thread statistics, can be placed here. It is said that this is a thread synchronization method, but it is obviously lock-free.

What are the points to consider when designing a spike system?

  1. Data warm-up spikes are all instantaneous operations, do not wait for traffic to come before loading data. The data can be warmed up in advance, such as loading it into the cache.
  2. Cache includes CDN cache and data cache. Ensure the high availability of the cache system, and then the data will land.
  3. Introduce MQ to solve oversold, serialize operation inventory, stop consumption after reaching the threshold, and close the purchase function. Or directly manipulate the cache.
  4. Traffic peak-cutting By introducing MQ, time-consuming services will be peak-cut to smoothly handle user needs.
  5. Fuse, current limiting and fuse, give priority to ensuring the main business. Limit flow, identify abnormal traffic, and block; at the same time, allow some requests to fail.
  6. When it is judged that the system load reaches the limit, the elastic expansion can resist the peak value by adding servers. The operation and maintenance environment needs to be opened up to be able to quickly expand.

to sum up:

The characteristics of talents that Internet giants prefer: enthusiasm for technology, strong technical foundation strength; initiative, good at teamwork, and good at summarizing and thinking. No matter which company it is, it attaches great importance to high-concurrency and high-availability technology and the foundation, so don't underestimate any knowledge. The interview is a two-way selection process. Don't go to the interview with a fearful attitude, which is not conducive to your own performance. At the same time, you should not only look at salary, but also whether you really like this company and whether you can really get exercise. In fact, I have written so much, but my own summary, not necessarily applicable to everyone, I believe that after some interviews, everyone will have these feelings.

In addition, if you want to answer the interview, please click on the 795983544 secret code CSDN to get it yourself. I also collected more than 20 years of company interview knowledge points and various technical points. Here are some screenshots I hope to help you.
Insert picture description here

Guess you like

Origin blog.csdn.net/banzhuanhu/article/details/108673138