当前位置:网站首页>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
边栏推荐
- 软件测试面试题:软件测试项目从什么时候开始?为什么?
- [numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
- Mysql 数据恢复流程 基于binlog redolog undolog
- Software test interview question: suppose there is a text box that requires the input of a 10 character postal code, how should the text box be divided into equivalent classes?
- Qt取出输入框字符串,lineEdit
- LVS+Keepalived高可用群集
- Common shortcut keys and setting methods of idea
- Mask automatic update description file (mask description file)
- It seems to be a bug of thread pool, but I think the source code design is unreasonable.
- 为什么要使用MQ消息中间件?这几个问题必须拿下
猜你喜欢
![[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)](/img/64/70fa9f47836e07dd41d9e283e50adb.jpg)
[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)

深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))

In addition to "adding machines", in fact, your micro service can be optimized like this

Small change project (two versions) with detailed ideas

LinkedList underlying source code

除了「加机器」,其实你的微服务还能这样优化

单核CPU, 1G内存,也能做JVM调优吗?

看起来是线程池的BUG,但是我认为是源码设计不合理。
![[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站崩了,那晚负责修复的开发人员做了什么?
随机推荐
Who is the sanctity of the six Chinese enterprises newly sanctioned by the United States?
It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
LInkedList底层源码
[day_4-review, basic concepts of objects and arrays - 1]
QT take out the input box string, lineedit
How to use Fiddler for weak network testing
深入理解递归的方法调用(含实例迷宫问题、汉诺塔、猴子吃桃、斐波拉契、阶乘))
看起来是线程池的BUG,但是我认为是源码设计不合理。
An article takes you into the world of pycharm - stop asking me about pycharm installation and environment configuration!!!
[question 22] dungeons and Warriors (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
一口气学完 Redis 集群方案
Oppo core making plan officially announced: the first chip or oppo M1
@The difference between Autowired annotation and @resource annotation
[C language] high precision addition, subtraction, multiplication and division template
MySQL execution process and order
The US Department of justice added 16 new charges against Huawei, including stealing trade secrets
How can anyone ask how MySQL archives data?
Pytoch distributed training
Yyds dry goods inventory # solve the real problem of famous enterprises: cycle number comparison
Idea connects to MySQL database and performs SQL query operations