当前位置:网站首页>What is modcount in the source code? What's the effect
What is modcount in the source code? What's the effect
2022-07-27 22:00:00 【Xiao Zhao, who is working hard for millions of annual salary】
Where can I see it
stay ArrayList,LinkedList,HashMap And so on , Delete , We can always see modCount The figure of
What does it mean
modCount, It literally means the number of changes
But why record modCount And the number of changes ?
Did you find a public feature , All use modCount Attribute It's all thread unsafe
that , We can't help thinking : This field is probably used to ensure thread safety
Read the source code , Only in The iterator corresponding to this data structure Only used in , With HashMap For example
How to use it
private abstract class HashIterator<E> implements Iterator<E> {
Entry<K,V> next; // next entry to return
int expectedModCount; // For fast-fail
int index; // current slot
Entry<K,V> current; // current entry
HashIterator() {
expectedModCount = modCount;
if (size > 0) {
// advance to first entry
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
}
public final boolean hasNext() {
return next != null;
}
final Entry<K,V> nextEntry() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Entry<K,V> e = next;
if (e == null)
throw new NoSuchElementException();
if ((next = e.next) == null) {
Entry[] t = table;
while (index < t.length && (next = t[index++]) == null)
;
}
current = e;
return e;
}
public void remove() {
if (current == null)
throw new IllegalStateException();
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
Object k = current.key;
current = null;
HashMap.this.removeEntryForKey(k);
expectedModCount = modCount;
}
}
As can be seen from the above code , At the beginning of an iterator, it will be given the object that calls the iterator modCount, If in the iterator traversal process , Once the object is found mcount And stored in iterators mcount Dissimilarity , Then throw the exception , It means that someone has modified it before I submit .
In fact, this is a kind of Java Unique rapid failure mechanism
Fail-Fast Mechanism
We know java.util.HashMap Not thread safe , So if there are other threads that have changed while using iterators map, So will throw ConcurrentModificationException, That's what's called fail-fast Strategy . This strategy is implemented in the source code through modCount Domain ,modCount As the name implies, the number of changes , Yes HashMap The content modification will increase this value , This value will be assigned to the iterator during iterator initialization expectedModCount. In the iteration process , Judge modCount Follow expectedModCount Whether it is equal or not , If it is not equal, it means that other threads have been modified Map: be aware modCount Declare as volatile, Ensure visibility of changes between threads .
So I'd like to suggest , When you traverse the non thread safe data structures , Try to use iterators
边栏推荐
- Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
- 零钱通项目(两个版本)含思路详解
- 8000字讲透OBSA原理与应用实践
- 为什么服务端程序都需要先 listen 一下
- Software testing interview question: when does the software testing project start? Why?
- Mask automatic update description file (mask description file)
- Idea connects to MySQL database and performs SQL query operations
- 声扬科技正式上线闻声远程声纹健康回访服务系统!
- 软件测试面试题:什么是回归测试?
- Cocoapods reload
猜你喜欢

Search, insert and delete of hash table
![[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/c4/71a9933a3a1fdd14f84a41b640f5b5.jpg)
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

B站崩了,如果我们是那晚负责修复的开发人员

University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l

聊聊 MySQL 事务二阶段提交

Station B collapsed. If we were the developer responsible for the repair that night

day 1 - day 4

@Can component be used on the same class as @bean?

看起来是线程池的BUG,但是我认为是源码设计不合理。

除了「加机器」,其实你的微服务还能这样优化
随机推荐
@Can component be used on the same class as @bean?
Yyds dry goods inventory # solve the real problem of famous enterprises: cycle number comparison
2021-11-05 understand main method syntax, code block and final keyword
Software test interview question: when saving a text file under windows, a save dialog box will pop up. If a test case is established for the file name, how should equivalent classes be divided?
软件测试面试题:软件测试项目从什么时候开始?为什么?
V2.x synchronization is abnormal. There are a lot of posts that cannot be synchronized in the cloud, and the synchronization is blocked and slow
Finish learning redis cluster solution at one go
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
Common shortcut keys and setting methods of idea
Technical practice behind bloom model: how to refine 176billion parameter model?
Search, insert and delete of hash table
Simple manual implementation of map
Ziguang zhanrui: dozens of 5g terminals based on chunteng 510 will be commercially available in 2020
疫情之下,手机供应链及线下渠道受阻!销量骤降库存严重!
Mask automatic update description file (mask description file)
CocoaPods 重装
How to deal with high concurrency deadlock?
Software testing interview question: how many strategies are there for system testing?
美司法部增加针对华为的指控,包括窃取商业秘密等16项新罪名
@RequestParam注解的详细介绍