当前位置:网站首页>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 !
边栏推荐
- 搜索引擎高级搜索方法记录
- Quantitative investment learning - Introduction to classic books
- SQL Server 基础介绍整理
- SSH, SCP command appears permission denied, please try again solution
- Pit record_ TreeSet custom sorting results in less data loss
- MySQL第十三次作业-事务管理
- MySQL项目8总结
- Procedure Call Standard
- 栖霞市住建局和消防救援大队开展消防安全培训
- Introduction to sysbench Basics
猜你喜欢

MySQL 9th job - connection Query & sub query

Idea remote debugger

量化投资学习——经典书籍介绍

Function run time

Quantitative investment learning - Introduction to classic books

MySQL seventh job - update data

Servlet learning notes II

MySQL第八次作业

MySQL第十二次作业-存储过程的应用

How to change the QR code material color of wechat applet
随机推荐
MySQL 8th job
搜索引擎高级搜索方法记录
AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D
Is it safe to open an account in the school of Finance and business?
Based on Zeng Shen's explanation, the line segment tree is studied again one
Jar version conflict resolution
See how I store integer data in the map < string, string > set
MySQL 9th job - connection Query & sub query
近期工作汇报
开发者,微服务架构到底是什么?
Linux下安裝Mysql【詳細】
基础-MySQL
MySQL job 11 - application de la vue
Record the handling of oom problems caused by too many threads at one time
Global and Chinese market for change and configuration management software 2022-2028: Research Report on technology, participants, trends, market size and share
Solution to the problem of compilation error due to repeated third-party package names
The difference between NPM and yarn
工作汇报(2)
Small example of SSM project, detailed tutorial of SSM integration
MySQL第七次作业-更新数据