当前位置:网站首页>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 !
边栏推荐
- 工作汇报(3)
- Global and Chinese market for baked potato chips 2022-2028: Research Report on technology, participants, trends, market size and share
- 工作汇报(2)
- ISO 26262之——2功能安全概念
- DataBinding使用与原理分析
- MySQL project 7 Summary
- Flutter and native communication (Part 1)
- 8-图文打造LeeCode算法宝典-最小栈与LRU缓存机制算法题解
- Getting started with postman
- Global and Chinese market for change and configuration management software 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Installing MySQL under Linux [details]
Nuxt. JS - learning notes
Easyexcel - Excel read / write tool
SSH, SCP command appears permission denied, please try again solution
MySQL第十一作業-視圖的應用
Express (I) - easy to get started
Jar version conflict resolution
ISO 26262之——2功能安全概念
开发者,微服务架构到底是什么?
MySQL 12th job - Application of stored procedure
随机推荐
See how I store integer data in the map < string, string > set
工作汇报(2)
Linux下安裝Mysql【詳細】
指南针软件买股票进行交易安全吗?怎么开户买股票
開發者,微服務架構到底是什麼?
Redis (basic) - learning notes
CentOS安装Redis多主多从集群
MySQL Chapter 4 Summary
Blog article index Summary - wechat games
[echart] i. how to learn echart and its characteristic document reading notes
Solution to the problem of compilation error due to repeated third-party package names
Little red book - Summary of internal sales spike project
Adaptiveavgpool2d does not support onnx export. Customize a class to replace adaptiveavgpool2d
Progressive web application PWA is the future of application development
哪些PHP开源作品值得关注
[depth first search] 312 Poke balloon
Swiftui development experience: data layer of application design for offline priority
Search engine advanced search method records
sysbench基础介绍
Flutter与原生通信(上)