java.util.ConcurrentModification…

java.util.ConcurrentModificationException异常原因和解决方法 简单地说就是你在遍历的时候,你自己不会去改变这个值,但是你在遍历的过程中发现这个值被改变了,那只有一种解释,其他人修改了集合导致这个值改变了。=expectedModCount导致异常。上面说集合类线程不安全,尝试调用工具类Collections.synchronizedList(dataList)将集合转换为线程安全的集合类,或是转换成CopyOnWriteArrayList,以为就可以解决问题了,很遗憾进行forEach插入的时候还是抛出了异常,想来这种安全的集合类也只能解决数组越界,数据丢失的问题。 阅读详情

错误代码:

public static void main(String[] args) {
  List list = new ArrayList();
  list.add("1");
  list.add("2");
  list.add("3");
  list.add("4");
  Iterator it = list.iterator();
  while (it.hasNext()) {
   if(it.next().equals("1")){
    list.remove("1");
   }
   
  }
 }

抛出异常:

java.util.ConcurrentModificationException
 at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
 at java.util.AbstractList$Itr.next(AbstractList.java:343)
 at com.baihe.util.ZnplUtil.main(ZnplUtil.java:256)

原因:

在内部类Itr中,有一个字段expectedModCount ,初始化

时等于modCount,即当我们调用list.iterator()返回迭代器时,该字段被初始化为等于modCount。在类Itr中next/remove方法都有调用checkForComodification()方法,在该方法中检测modCount == expectedModCount,如果不相当则抛出异常ConcurrentModificationException。
前面说过,在集合的修改操作(add/remove)中,都对modCount操作了。
在看看刚开始提出的那段代码,在迭代过程中,执行list.remove(val),使得modCount+1,当下一次循环时,执行 it.next(),checkForComodification方法发现modCount != expectedModCount,则抛出异常。

解决办法:

如果想要在迭代的过程中,执行删除元素操作怎么办?
再来看看内部类Itr的remove()方法,在删除元素后,有这么一句expectedModCount = modCount,同步修改expectedModCount 的值。所以,如果需要在使用迭代器迭代时,删除元素,可以使用迭代器提供的remove方法。对于add操作,则在整个迭代器迭代过程中是不允许的。 其他集合(Map/Set)使用迭代器迭代也是一样。



用 SemanticObject 把字段变成可点击链接:Fiori Elements Navigation Using a Link 的工程化实践 SAP Fiori elements 通过 Common.SemanticObject 注解可将字段转为可点击链接,实现应用间跳转。当源字段名与目标属性名不一致时,需使用 Common.SemanticObjectMapping 进行映射。该功能依赖 Fiori Launchpad 的目标映射配置,确保正确解析 semantic object + action 并传递参数。常见误区包括未配置目标映射或字段映射错误,需逐一检查语义标注、intent 解析和参数传递链路。 阅读详情

相关推荐

Lenovo 拯救者15ISK硬件升级实战:从内存到固态硬盘的全面优化

本文详细介绍了为联想拯救者15ISK笔记本电脑进行硬件升级的完整实战指南。通过评估升级可行性,指导如何挑选兼容的内存条与NVMe固态硬盘,并提供详细的拆机安装步骤与系统安装避坑技巧。升级后,笔记本的开机速度、软件加载及多任务处理能力获得飞跃式提升,让老旧设备重获新生。

weixin_29310631的博客 556

为什么会出现ConcurrentModificationException异常,如何解决?

在前面一篇文章中提到,对Vector、ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常。下面我们就来讨论以下这个异常出现的原因以及解决办法。 1、异常出现的原因 先看下面这段代码: public class Test { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList

掘客DIGGKR 2952

hcaptcha-challenger:基于本地AI模型自动破解验证码的工程实践

验证码作为区分人类与机器的经典安全机制,其核心原理是通过图像识别、行为分析等交互式挑战来阻挡自动化程序。传统绕过方法依赖第三方打码平台或脚本,存在成本高、稳定性差的问题。随着多模态大语言模型和计算机视觉技术的发展,本地化AI求解成为新的技术方向,它通过集成YOLO、ResNet等模型,在本地完成图像语义理解,实现了高隐私、低成本、可进化的验证码自动化解决方案。这种范式尤其适用于需要大规模稳定处理网页交互的场景,如电商数据采集和社交媒体管理。hcaptcha-challenger项目正是这一理念的工程实践,它

weixin_42587866的博客 1025

java.util.ConcurrentModificationException详解

一、异常产生 当我们迭代一个ArrayList或者HashMap时,如果尝试对集合做一些修改操作(例如删除元素),可能会抛出java.util.ConcurrentModificationException的异常。 import java.util.Iterator; import java.util.List; public class AddRemoveListElement { public static void main(String args[]) { List&l

分享Android开发知识 8868

MATLAB三元相图实战:从数据准备到高级可视化技巧全解析

本文深入解析了如何使用MATLAB绘制专业的三元相图(ternary plot)。文章从数据归一化、范围映射等预处理技巧入手,详细介绍了散点图、等高线图、凸包计算等高级可视化方法,并提供了多数据集对比与出版级图表美化的实战指南,帮助科研与工程人员高效呈现复杂的三元数据关系。

spark7igniter的博客 258

已解决java.util.ConcurrentModificationException异常的正确解决方法,亲测有效!!!

已解决java.util.ConcurrentModificationException异常的正确解决方法,亲测有效!!!

小明的Java问道之路 4602

point transformer v3结构

分割结构: embedding->enc->dec embedding->enc0->enc1->enc2->enc3->enc4->dec3->dec2->dec1->dec0 embedding ->block0->block1 ->dow->block0->block1 ->dow->block0->block1 ->dow->block0->block1-&gt

weixin_45625419的博客 581

Java异常:Java.util.ConcurrentModificationException异常处理

在开发中使用了迭代器对 List 进行遍历,遇到了java.util.ConcurrentModificationException报错,本文记录了这个错误出现的原因和如何解决。

qq_46119575的博客 1万+

高斯投影坐标正反算算法(python)

高斯坐标正反算算法,在正算的时候,y的第一个数字表示是用几度带换算的,第二个数字和第三个数字表示带号。该程序是根据武汉大学出版社出版的第三版的《大地测量学基础》编写, 用于课程实验学习。

weixin_64338372的博客 4588

由Sensor光谱响应曲线联想到的白平衡增益计算

由Sensor光谱响应曲线联想到的白平衡增益计算 由于传感器硬件的限制,传感器在某个预定波长范围内的响应不可能是100% 响应。现实中都是类似于正态分布的单峰函数。如果仅做粗略计算,由一个中心波长和半高宽(也成半波宽)完全可以由数学公式计算出一个高斯分布曲线。这个分布曲线就叫波谱响应函数。 波谱响应函数反映了传感器通道获取能量的情况。实际上每个传感器厂商都提供了具体的光谱响应函数,一般由两列数据组成,一列数据是波长,另一列数据是相对响应强度。下图某sensor光谱的响应的波谱响应函数。 ...

大熊 7490

出现了java.util.ConcurrentModificationException异常以及解决方案

import java.util.ArrayList; import java.util.List; import java.util.UUID; public class ContainerNotSafeDemo { public static void main(String[] args) { List<String> list= new ArrayList<>(); for (int i = 0; i < 30; i++) { .

weixin_57109001的博客 1万+

【ROS2】从yaml文件中读取参数配置

ROS2:从yaml中读取参数,到节点;使用C++演示参数的增、删、改、查、遍历、监听变化

郭老二 1284

java.util.ConcurrentModification并发修改异常

在运行下面这段代码时出现了并发修改异常java.util.ConcurrentModification: public static void main(String[] args) { List l = new ArrayList(); l.add(100); l.add(200); l.add(300); ...

weixin_30522095的博客 155

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

日常采坑 --- Exception in thread "java.util.concurrent.ThreadFactory57" java.util.ConcurrentModification

非线程安全的map在多线程环境中运行出错。 Exception in thread "java.util.concurrent.ThreadFactory57" java.util.ConcurrentModificationException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.ja...

qq_41497111的博客 540

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

快速失败(fail-fast)和安全失败(fail-safe)的区别是什么?

安全失败:你在迭代的时候会去底层集合做一个拷贝,所以你在修改上层集合的时候是不会受影响的,不会抛出ConcurrentModification异常。快速失败:当你在迭代一个集合的时候,如果有另一个线程正在修改你正在访问的那个集合时,就会抛出一个ConcurrentModification异常。在java.util.concurrent包下的全是安全失败的。在java.util包下的都是快速失败。

qq_61141142的博客 206

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Java 快速失败( fail-fast ) 安全失败( fail-safe )

原文:http://www.cnblogs.com/ygj0930/p/6543350.html 快速失败( fail-fast ):当你在迭代一个集合的时候,如果有另一个线程正在修改你正在访问的那个集合时,就会抛出一个ConcurrentModification异常。 在java.util包下的都是快速失败。 安全失败( fail-safe ):你在迭代的时候会去底层集合做...

weixin_30502965的博客 79

java.util.ConcurrentModificationException解决

   今天组长提了一个问题注意点,说删除Set元素的时候,如果按照一下代码则会抛出异常:    SetObject> bb = new HashSetObject>();    bb.add("23242");    bb.add(Integer.valueOf(45));    Iterator it = bb.iterator();    while(it.hasNext()) {  

5544

java.util.ConcurrentModificationException: null 报错解决

ConcurrentModificationException报错解决及相应的List知识扩展

qq_51741039的博客 3万+

Java 集合总结以及 java.util.ConcurrentModificationException 异常分析

前言 在开发中,经常使用的集合就是 ArrayList 和 HashMap,在一些需要排序的场景中,可能需要使用 TreeSet 和 TreeMap 等,其余的集合类有的由于长时间不适用,已经忘记了其特性和应用场景,所以在这里重新总结一下,方便以后复习。 Java 集合体系 ...

wjavadog的博客 313

java.util.ConcurrentModificationException 报错

近日在项目中遇见java.util.ConcurrentModificationException报错......感觉是一个比较经典的错误,做个笔记分享一下。 使用for循环+list.remove();造成的。 经过如下测试,项目中使用JDK1.6出现的问题,我使用JDK1.8测试没有问题。故我判断可能和JDK版本有关系。如果你也遇到同样的问题,最好在自己的环境中测试一下。 下面是一...

思念十年后 778

hashMap异常:java.util.ConcurrentModificationException

hashMap异常:java.util.ConcurrentModificationException

p15097962069的博客 137

java.util.ConcurrentModificationException: null出现原因及解决方法

原因:Iterator做遍历的时候,HashMap被修改(bb.remove(ele), size-1),Iterator(Object ele=it.next())会检查HashMap的size,size发生变化,抛出错误ConcurrentModificationException。 // 1 使用Iterator提供的remove方法,用于删除当前元素 for (Iterator it = ...

qq_41969665的博客 1万+

异常:java.util.ConcurrentModificationException

简单的小代码贴上:public class Test { public static void main(String args[]) { ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;(); list.add("1"); list.add("2"); list.add("3"); list.add("4...

掉了根毛的博客 261

java.util.ConcurrentModificationException: null异常(已解决,原因出自于Java集合的快速失败机制)

今天我在写项目时,对集合中的元素做了以下操作在Java中对集合进行遍历时,一旦对集合进行增删操作,在遍历时会引起java.util.ConcurrentModificationException: null异常。

记录开发中常见的问题以及技术等 8088

java.util.ConcurrentModificationException

java.util.ConcurrentModificationException异常(转) 1、 今天在写一个带缓存功能的访问代理程序时出现了java.util.ConcurrentModificationException异常,因为该异常是非捕获型异常而且很少见,所以费了些时间才找到问题所在,原来在通过Iterator进行遍历的时候,如果直接对HashMap进行操作后,再继续用之前的I...

MoneyEastSea的博客 156
上一篇: 一句话证明你是程序员
下一篇: hibernate实体类配置详解
jarry.liu
博客等级 码龄16年 36粉丝 304原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jarry.liu

如果对您有帮助,鼓励下博主吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值