当前位置:网站首页>2020.7.6 interview with fence network technology company

2020.7.6 interview with fence network technology company

2022-06-26 10:47:00 Pig man blogs

Java How to ensure thread safety in

What is thread safety :

When multiple threads access a method , No matter how you call it 、 Or how these threads execute alternately , We don't need to do any synchronization in the main program , The resulting behavior of this class is the right behavior we assume , We can say that this class is thread safe .

1. First of all, I will explain the three features of concurrent programming

  • Atomicity

  • visibility

  • Orderliness

2. In this paper, we describe the methods to ensure the safety of concurrent programming

1. Use atomic**** class

 The bottom layer has the help of UnSafe Class provides the CAS The operation can ensure that the data is thread safe when it is updated 

explain CAS:

CAS The operation consists of three operands : Memory location to read and write (V)、 The original value of the expected (A)、 The new value (B). If the memory location is the same as the expected original value A Match , Then update the value of the memory location to the new value B. If the memory location does not match the expected original value , So the processor doesn't do anything . In either case , It will be in CAS Command to return the value of the location before .

Operation principle :

Conflict detection and data update . When multiple threads are trying to use CAS When updating the same variable at the same time , Only one thread can update the value of a variable , Other threads will fail , Failed threads do not hang , But to tell you that you have failed in this competition , And try again .

2. Lock synchronized and lock

3. Add to variable final keyword

The difference between interceptors and filters :

1. filter :

Depend on servlet Containers . Implementation based on function callbacks , Almost any request can be filtered , The drawback is that a filter instance can only be invoked once when the container is initialized . The purpose of using filters is to do some filtering , Get the data we want to get , such as : Modify the character encoding in the filter ; Modify in the filter HttpServletRequest Some parameters of , Include : Filter vulgar text 、 Dangerous characters, etc

2. Interceptor :

Depend on web frame , stay SpringMVC Omega is dependent on omega SpringMVC frame . Based on the implementation Java The reflection mechanism of , Section oriented programming (AOP) An application of . Because the interceptor is based on web Framework call , So you can use Spring Dependency injection of (DI) Do some business operations , At the same time one interceptor instance in one controller It can be called multiple times during the life cycle . But the disadvantage is that it can only be right controller Request interception , Other requests, such as direct access to static resources, cannot be intercepted

summary :

1. The interceptor is based on java The reflection mechanism of , And filters are based on function callbacks .

2. Interceptors don't rely on servlet Containers , The filter depends on servlet Containers .

3. Interceptors can only be used to action The request works , Filters work on almost all requests .

4. Interceptors can access action Context 、 Value the objects in the stack , And the filter can't access .

5. stay action In the life cycle of , Interceptors can be called many times , The filter can only be called once when the container is initialized

About solving the problem of multiple execution of scheduled tasks in a distributed cluster environment

problem : Because our project is deployed on multiple cluster machines at the same time , Therefore, when the specified timing time is reached , Timers on multiple machines may start at the same time , Cause problems such as duplicate data or program exceptions ,

1. With the help of Redis And distributed locks

** resolvent :** For your timer in Redis Define a key value pair in , You can use the project name and server ip, Start with... Before carrying out the task Redis Medium read key , If there is no value, it means that the task has not been executed , The same machine is updated first redis, Then trigger the scheduled task . because Redis There is an expiration mechanism , Therefore, the expiration time can be set to ensure that the next judgment is normal

** Advantages and disadvantages :** This method is recommended personally , Simple , Changes to business logic will also be much less , Just add a simple judgment to the original timer

redis The idea of storing hot data ?

1. The user clicks on the product , Store the product hits in redis In the library , Reuse redis The elimination strategy of traditional Chinese Medicine

2. When redis When the memory used exceeds the set maximum memory , Will trigger redis Of key Elimination mechanism , stay redis 3.0 There is 6 Kind of Elimination strategy

  • **noeviction: Do not delete policy :** When the maximum memory limit is reached , If you need to use more memory , The error message is returned directly .(redis Default elimination strategy )

  • allkeys-lru: In all key Priority to delete the least recently used (less recently used ,LRU) Of key.

  • allkeys-random: In all key Randomly delete a part of the key.

  • volatile-lru: After setting the timeout (expire ) Of key Priority to delete the least recently used (less recently used ,LRU) Of key.

  • volatile-random: After setting the timeout (expire) Of key Randomly delete a part of the key.

  • volatile-ttl: After setting the timeout (expire ) Of key Priority to delete the remaining time (time to live,TTL) A short key.

If you can think of a better way , Welcome to leave a message below !

原网站

版权声明
本文为[Pig man blogs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170528339258.html