2 years java, ant side, Death

In fact, I did not answer it a. Not because I am stupid because I will not. With the help of a large disturbance, and now I will, and I beg you to give me a chance.

TreeSet / HashSet difference

As the name suggests, is the first structurally different

1, the structure TreeSet behind TreeMap, that is, red-black tree can be automatically sorted. It compares the content or by the equals method compareTo method.

2, is behind HashSet HashMap, key is disordered, can only do external sorting. Since it is a Hash, then they would have to rewrite hashCode and equals method in which the object

In addition, there is a slight difference can be used to install b:

1, HashSet can accept a null value, and there is only a 2, TreeSet 默认unacceptable value is null, null pointer exception will be thrown directly

There is no set of duplicate data, TreeSet there are not even nothingness.

HashMap how to resolve the conflict, the expansion mechanism

Issue bad street, which asked what answer it. Such a thing is back.

In fact, the internal structure of an array HashMap + list (if the length is greater than 8 java8 converted into a red-black tree). When HashMap initialization, the default 16 hash槽.

When an object is stored, first, the object hashCode, targeting hash slot. If multiple objects at the same time fall into the same slot, then the list will be used to resolve conflicts on this slot.

When you create a HashMap, there will be a load factor. Each put operation, checks whether the current capacity exceeds a threshold value (initailCapacity * loadFactor). If exceeded, the expansion of the current twice. After the expansion, the data need to re-hash, which is the transfermethod.

Experience: resize very time-consuming, so if we can estimate capacity in advance, can initailCapacity fixed in advance.

How do ConcurrentHashMap high concurrency

Simply put, using segmented lock (lock separation). Each part of the data lock for locking the container to reduce the competition between thread lock.

This question was asked to the deep will be dead, space is limited, not long-winded.

How to use common thread pool

Ordinary scenes, using Executors factory class created it. Commonly used Single, Fixed, Cached three.

More often, for more granular control, directly to ThreadPoolExecutor class customization. Ali norms also require such a practice (of course, to lick), which I am particularly concerned about blocking queue and saturation strategy.

Of course, you can only say that the policy of denial and blocking queue familiar. Otherwise, do not give yourself digging too smart.

They love you mentioned Ali specification, which made me feel very low jdk design

Multiple threads waiting to release a unified node then there are several ways to achieve?

The most classic is CountDownLatch, the main thread is blocked in await method, each thread calls countDown. You can solve some classic race problem.

Another variant is CyclicBarrier. Each thread blocked in await method, up to a certain threshold collective release.

You also can use some more junior api, such as Thread join method. Future's get method. Complex is not recommended.

A sleep can also ah. is there a problem? I use while waiting for a variable is possible, but why should I do that?

Database index structure

Tree + B , slow to adapt a disk born index structure. Must ensure that the queries in the leftmost prefix of the index.

Hash and HashMap similar way to deal with conflicts list

pg index structure will more to go. Mysql so little how I feel strange? Do you want me to answer the difference between the storage engine?

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

I know this conclusion on the line => When the order by fields appear where conditions, will use the index without sorting operations. Other cases, order by sorting operation does not appear.

According to the leftmost principle, I can create (a, b) index.

## What is the clustered index and non-clustered indexes

A table can have only one clustered index. Main index and data files to the same document, the default InnoDB to support a clustered index, B + data on Tree leaf node is the data itself.

The MyISAM does not support clustered index, it's not a leaf node stored data itself, but the address data is stored. In the file structure it will be divided into an index file, a data file.

For programming, with no birds.

CAP understand it? Redis in the CAP What?

  • Consistency consistency
  • Availability Availability
  • Partition tolerance Fault-tolerant general partition, are tradeoffs between C, A.

redis focus on simple master-slave mode CP, i.e., the higher the requirements for uniformity. redis-cluster, belongs to APtype, a greater emphasis on usability

cap is a hat, the kind of green

How to understand the power and so on? Projects such as power interfaces is how to do?

Idempotent refers multiple times, the same impact.

For example, most of the Post operation, resubmit orders, etc., you will eventually have an order to generate success. Another case is the message, because the guarantee most of MQ at least once, so sometimes the message will be repeated.

1, the request for the Post, I usually after the request is successful, forced to jump to other pages, to avoid refresh submitted.

2, a complicated operation is generally used to achieve serial number.

3, some messages with no serial number, processing time, it is necessary Check multiple checksums, message status table even introduced to ensure idempotent.

Like confession, confession every time are being rejected because I was that id!

Under optimistic locking Pessimistic locking explanation

Pessimistic locking always assume the worst case, each operation data that others will modify it to ensure the safety lock. Behind the visitor can only wait. Row lock the database, table locks, java synchronization keywords, etc., are all pessimistic locking.

Optimistic locking the contrary, always assume the best case, without locking the data, but more than an extra judge operation. For example a large number of concurrent bag CAS operation, determines the version number of the old and new mechanism.

Pessimistic locking is my wife, you have exclusive; optimistic locking is booty, by appointment planning

JVM determine whether the object is reclaimed?

The answer is GC roots. That is, starting from the root object, not a single object reference to it, it is judged that the target is unreachable.

It is often scolded "die without sons" who is to be recycled root

GCROOT What?

1, the virtual machine stack (Local Variable Table stack frame) referenced object.

2, the JNI native method stack (i.e., the general said native method) referenced object.

3, the method area static variables and constants referenced object.

4, active threads referenced objects

So do not let them overgrowth.

Reflection can obtain the name of the class inside the method do? Parameter name it? Parameter type it?

All OK.

After java8, obtaining parameters Parameter class 名称. But the premise, and needs to compile switch.

javac -parameters
复制代码

The default is off, dry!

Problems are partial to the moon

Dynamic proxy implementations? CgLib and jdk proxy What is the difference?

java be achieved by implementing dynamic proxy InvocationHandler interfaces, then initialize the Proxy.

Cglib using ASM it generated its own framework, can proxy the general category, but not final class of agents, but only jdk agent interface.

In the spring there, cglib win

What are distributed lock mainstream implementation? Redis and zk lock What is the difference?

Divided into two categories.

Optimistic locking: Based on the version number and CAS mechanism to achieve, regardless of the store holds the version number.

Pessimistic locking: 1, based on a database record, write data entry, delete records on exit

2, the database row lock, such as distributed quartz, which is an exclusive lock

3, based on the Redis setnx function (since most of the time out is set, it is recommended to set the tape px atomic functions)

4, based zookeeper

the difference:

redis acquiring the lock mechanism is in rotation. After the lock is released there will be more competition for the caller, there are some tasks might starve to death.

zk is listening mechanism, changes will be notified. In addition to non-fair locks, you can also achieve a fair locks.

From the elegance, it is clear redis win

What ThreadLocal role? Say under usage

ThreadLocal used to isolate the data. ThreadLocal stored in the data associated with the thread, the bottom is actually a map, can be acquired by the map data stored in the thread.

This embodiment is similar to the Servlet in the Request.

Some need to be bound to a data threads, some threads such as statistical data, it can be placed here.

It is said that this is a thread synchronization way, but it's obviously no lock ah.

There is no way to optimize ThreadLocal?

Map ThreadLocal in poor performance, a method for solving linear detection Hash employed.

Netty on it was tuned to optimize the way inherited the Thread class, to achieve their FastThreadLocal. It uses

Do not understand jdk, obviously there are O (1) of the Map, have to make themselves a slower wheel, why? Saying this, but just partial to Mars.

Design spike system which point to consider?

1, data pre- spike is an instantaneous operation, do not wait to re-load the data flow. Data can be preheated in advance, such as loaded into the cache and so on.

2, the cache comprising a CDN and data caches. Caching system to ensure high availability, data is then ground.

3, to solve the oversold introduced MQ, serialized inventory operation, after reaching the threshold value is no longer consume, and close the purchase function. Cache operation or directly.

4, flow clipping , clipping will be time-consuming business by introducing MQ, smooth handling customer needs.

5, current-limiting fuse is blown, the main priority to ensure the conduct of business. Limiting, to identify abnormal traffic blockade; while allowing the part of the request fails.

6, the elastic expansion of the system is determined when the load reaches the limit, can resist the peak by a route server increases. We need to get through the operation and maintenance environment, be able to rapid expansion.

Afraid afraid to ask a little catch in the end. Spike ass ah, Taobao spike either grab or 500!

Well, I do not want to Hangzhou. Try the next Ali worst department to see if the requirements are not low, you can not be too.

Guess you like

Origin juejin.im/post/5cf79083f265da1b957045d1