I didn’t answer any of them. It’s not because I’m stupid, it’s because I can’t. With the help of Da Haru, I can do it now. Please give me another chance.
TreeSet/HashSet difference
1. The structure behind TreeSet is TreeMap, also known as red black tree, which can implement automatic sorting. It compares content using either the Equals or compareTo methods. 2. The HashMap is behind the HashSet. The key is unordered and can only be sorted externally. Since it is a Hash, we need to override the hashCode and equals methods of the objects in it
TreeSet does not accept null values by default. Instead, TreeSet does not accept null values
There are no duplicates in a set, no nothingness in a TreeSet.
How does HashMap resolve conflicts and expand capacity
You can answer the fucking street question. Such things are called backrests.
The internal structure of a HashMap is actually an array + a linked list (java8 will convert to a red-black tree if the length is greater than 8). When a HashMap is initialized, it has 16 hash slots by default. When storing an object, the hash slot is first located through the object’s hashCode. If multiple objects fall into the same slot at the same time, a linked list is used to resolve conflicts on that slot. When a HashMap is created, it has a load factor. Each put operation checks whether the current capacity exceeds the threshold (initailCapacity*loadFactor). If it exceeds, it will be doubled. After capacity expansion, data needs to be hashed again, which is the transfer method.
Rule of thumb: Resize is time-consuming, so if you can predict capacity in advance, you can fix initailCapacity in advance.
How does ConcurrentHashMap achieve high concurrency
In simple terms, segmented locks are used. Each lock is used to hold a portion of the data in the container, reducing lock contention between threads.
This topic to deep ask will be dead, space is limited, not wordy.
How do thread pools work
For a common scenario, use the factory Executors to create it. Single, Fixed and Cached are commonly used. More often, the ThreadPoolExecutor class is customized directly for finer control. Ali’s specification also requires this (lick it, of course), and I’m particularly concerned about blocking queues and saturation strategies. Of course, you can only say this if you are familiar with blocking queues and rejection policies. It’s not smart to dig a hole for yourself.
They liked your mention of the Ali specification, which made me think the JDK was poorly designed
How many ways can multiple threads wait for a node and then release it uniformly?
The most classic is CountDownLatch, where the main thread blocks in the await method and every thread calls countDown. Could solve some classic racing problems. Another variant is CyclicBarrier. Each thread blocks in the await method and blocks to a certain threshold. More primitive apis, such as the Join method for Thread, are also available. Future get method, etc. Complex is not recommended.
You can also say sleep. Any questions? I could have waited for a variable while, but why should I?
Database index structure
B+ Tree, an index structure designed to accommodate slow disks. You must ensure that the index is queried by the left-most prefix. Hash is similar to HashMap in that it handles collisions in a linked list
There are a lot of index structures for PG. Why does it feel weird to have so little Mysql? Do I have to answer the difference between storage engines?
select * from t where a=? and b>? Order by c limit 0,100
If the order by field is present in the WHERE condition, the index is utilized without the sorting operation. In other cases, the order by operation does not appear.
On the left, I can create an index of (a,b).
What are clustered and non-clustered indexes
A table can have only one clustered index. The main index file and the data file are the same file, the default InnoDB supports clustered index, data on the leaf node of B+ Tree is the data itself.
MyISAM, on the other hand, 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 can be divided into an index file and a data file.
It’s useless for programming.
Do you know CAP? What is CAP in Redis?
- Consistency Consistency
- The Availability Availability
- Generally, A trade-off is carried out between C and A.
Redis simple master-slave mode focuses on CP, that is, it has high requirements for consistency. Redis-cluster, which is of the AP type, places more emphasis on availability
A cap is a green hat
How to understand idempotent? How are interfaces idempotent in projects?
Idempotent means that it is executed multiple times with the same effect.
For example, in most Post operations, repeat order submission, and so on, only one order will be generated successfully. Another situation is messages, which are sometimes repeated because most MQ guarantees at least once.
1. For Post requests, I usually force a jump to another page after a successful request to avoid refreshing the submission. 2. Complex operations are usually implemented using serial numbers. 3. Some messages without serial numbers need to be checked several times, and even the message status table is introduced to ensure idempotent.
Just like confession, every confession is rejected, because I am that ID!
Explain the optimistic lock pessimistic lock
Pessimistic locking always assumes the worst, and every time you manipulate data, you assume someone else will change it, so you lock it to ensure security. The next visitors can only wait. Row locks and table locks in databases and synchronization keys in Java are pessimistic locks.
Optimistic locking is the opposite, always assuming the best, without locking the data, but with an extra judgment operation. For example, the large number of CAS operations in the Concurrent package, the mechanism for determining old and new version numbers, etc.
Pessimistic lock is the wife, you have exclusive; Optimistic lock is a booty call. By appointment
Does the JVM determine whether an object is recycled?
GC Roots. If the root object is not referenced by any other object, the object is considered unreachable.
The person who is often called “childless” is the root who needs to be reclaimed
What does GCROOT have?
1. Objects referenced in the virtual machine stack (local variable table in the stack frame). 2. Objects referenced by JNI (generally called native methods) in the native method stack. Objects referenced by static variables and constants in the method area. 4. Reference objects for active threads
So don’t let them overbreed.
Can reflection get the name of a method in a class? What about the parameter name? What about parameter types?
Will do.
After Java8, get the Parameter name from the Parameter class. But there is a premise, need to add a compiler switch.
javac -parameters
Copy the code
Copy code is off by default, dry!
The problem is all going to the moon
How to implement dynamic proxy? What is the difference between CgLib and JDK proxies?
Java implements dynamic proxies by implementing the InvocationHandler interface, and then initializes them using proxies. Cglib uses its own ASM generation framework, which can proxy ordinary classes, but not final classes, whereas the JDK can only proxy interfaces.
In Spring, Cglib wins
What are the main implementation methods of distributed lock? What is the difference between Redis and ZK locks?
There are two general categories.
Optimistic lock: Based on the version number mechanism and CAS implementation, regardless of the storage of the version number.
Pessimistic lock: 1, based on the database record, write data on entry, delete records on exit 2, database row lock, such as distributed Quartz, which is an exclusive lock 3, based on Redis setnx function (because most of the set timeout, so the recommended use of PX atomic function) 4, based on ZooKeeper
Difference: Redis lock acquisition is a rotation mechanism. When the lock is released, multiple callers fight over it, and certain tasks may starve to death. Zk is a listening mechanism and will be notified of any changes. In addition to unfair locking, fair locking can also be implemented.
On the merits, redis clearly wins
What does ThreadLocal do? Said the usage
ThreadLocal is used to isolate data. ThreadLocal stores thread-specific data, and the underlying layer is actually a map that can be retrieved from the thread.
This approach is similar to Request in servlets.
Some data that needs to be bound to the thread, such as some thread statistics, can be placed here.
This is said to be a thread synchronization method, but it is obviously unlocked.
Is there a way to optimize ThreadLocal?
The Map performance of ThreadLocal is poor, so the linear detection method of Hash is solved. Netty optimized it by inheriting Thread and implementing its own FastThreadLocal. It USES
I don’t understand the JDK, I have O(1) Map, but I have to build a slower wheel. Why? This question, by the way, goes to Mars.
What points should be considered in designing a seckill system?
1, data preheating seconds are instantaneous operation, do not wait for the flow to load data. You can preheat the data, such as loading it into a cache. 2. Cache includes CDN cache and data cache. Ensure that the cache system is highly available and the data lands later. 3. Solve the oversold problem, introduce MQ, serialize the inventory, no longer consume after reaching the threshold, and close the purchase function. Or operate the cache directly. 4. Peak flow clipping By introducing MQ, time consuming services can be clipped to handle user needs smoothly. 5, fuse current limit fuse, priority to ensure the main business. Limit the flow, identify abnormal flow and block it; At the same time, some requests are allowed to fail. 6. Elastic capacity expansion When the system load reaches its limit, it can resist peak value by adding servers. The o&M environment needs to be cleared for rapid expansion.
Afraid afraid to grasp a point to ask the end. Second to kill a fart, Taobao’s second to kill or grab, or 500!
Forget it. We don’t want to go to Hangzhou either. Next time try Ali’s worst department and see if the requirements are low and can be passed.