当前位置:网站首页>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 !
边栏推荐
- MySQL 9th job - connection Query & sub query
- OpenCV图像处理-灰度处理
- 【在线仿真】Arduino UNO PWM 控制直流电机转速
- Windows and Linux regularly backup MySQL database
- Pit record_ TreeSet custom sorting results in less data loss
- Bit operation n & (n-1), leetcode231, interview question 05.06
- Redis (IV) redis association table caching
- MySQL第四章总结
- 3行3列整形二维数组,求对角之和
- 搜索引擎高级搜索方法记录
猜你喜欢
![[echart] II. User manual and configuration item reading notes](/img/03/fa87e5b8f8d23381ea6923d507f250.jpg)
[echart] II. User manual and configuration item reading notes

DataBinding使用与原理分析

Vscode environment setup: synchronous configuration

MySQL第十四次作业--电子商城项目

MySQL 8th job

AdaptiveAvgPool2D 不支持 onnx 导出,自定义一个类代替 AdaptiveAvgPool2D

開發者,微服務架構到底是什麼?

MySQL第十一作业-视图的应用

Nuxt. JS - learning notes

Little red book - Summary of internal sales spike project
随机推荐
Global and Chinese market for change and configuration management software 2022-2028: Research Report on technology, participants, trends, market size and share
Based on Zeng Shen's explanation, the line segment tree is studied again one
小笔记-简单但够用系列_KVM快速入门
MySQL 8th job
Oracle sqlplus query result display optimization
AIX基本操作记录
Redis (IV) redis association table caching
MySQL job 11 - application de la vue
JS reverse | four libraries and one platform response data encryption
工作汇报(2)
Windows and Linux regularly backup MySQL database
Flutter and native communication (Part 1)
Servlet learning notes II
六月集训(第26天) —— 并查集
Renesas electronics launched a complete intelligent sensor solution for Internet of things applications
Global and Chinese markets of children's electronic thermometers 2022-2028: Research Report on technology, participants, trends, market size and share
ISO 26262之——2功能安全概念
Pit record_ TreeSet custom sorting results in less data loss
SQL index learning notes
MySQL Chapter 6 Summary