首页 文章 精选 留言 我的

精选列表

搜索[Java生态],共10000篇文章
优秀的个人博客,低调大师

Java 集合中常见 checkForComodification()方法的作用? modCount和expectedModCount作用?

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40254498/article/details/81386920 主要是用来实现fail-fast机制 有两个线程(线程A,线程B),其中线程A负责遍历list、线程B修改list。 -线程A在遍历list过程的某个时候(此时expectedModCount = modCount=N),线程启动, 同时线程B增加一个元素,这是modCount的值发生改变(modCount + 1 = N + 1)。 线程A继续遍历执行next方法时, 通告checkForComodification方法发现expectedModCount = N , 而modCount = N + 1,两者不等, 这时就抛出ConcurrentModificationException 异常,从而产生fail-fast机制。 /** * 当前取的是 JDK1.8 ArrayList中的代码 */ /** * The number of times this list has been <i>structurally modified</i>. * Structural modifications are those that change the size of the * list, or otherwise perturb it in such a fashion that iterations in * progress may yield incorrect results. * * <p>This field is used by the iterator and list iterator implementation * returned by the {@code iterator} and {@code listIterator} methods. * If the value of this field changes unexpectedly, the iterator (or list * iterator) will throw a {@code ConcurrentModificationException} in * response to the {@code next}, {@code remove}, {@code previous}, * {@code set} or {@code add} operations. This provides * <i>fail-fast</i> behavior, rather than non-deterministic behavior in * the face of concurrent modification during iteration. * * <p><b>Use of this field by subclasses is optional.</b> If a subclass * wishes to provide fail-fast iterators (and list iterators), then it * merely has to increment this field in its {@code add(int, E)} and * {@code remove(int)} methods (and any other methods that it overrides * that result in structural modifications to the list). A single call to * {@code add(int, E)} or {@code remove(int)} must add no more than * one to this field, or the iterators (and list iterators) will throw * bogus {@code ConcurrentModificationExceptions}. If an implementation * does not wish to provide fail-fast iterators, this field may be * ignored. */ protected transient int modCount = 0; 在父类AbstractList中定义了一个int型的属性:modCount protected transient int modCount = 0; 在ArrayList的所有涉及结构变化的方法中都增加modCount的值,包括:add()、remove()、addAll()、removeRange()及clear()方法。这些方法每调用一次,modCount的值就加1。注:add()及addAll()方法的modCount的值是在其中调用的ensureCapacity()方法中增加的。 AbstractList中的iterator()方法(ArrayList直接继承了这个方法)使用了一个私有内部成员类Itr,生成一个Itr对象(Iterator接口)返回: public Iterator iterator() { return new Itr(); } Itr实现了Iterator()接口,其中也定义了一个int型的属性:expectedModCount,这个属性在Itr类初始化时被赋予ArrayList对象的modCount属性的值。 int expectedModCount = modCount; 注:内部成员类Itr也是ArrayList类的一个成员,它可以访问所有的AbstractList的属性和方法。理解了这一点,Itr类的实现就容易理解了。 在Itr.hasNext()方法中: public boolean hasNext() { return cursor != size; } 调用了AbstractList的size,比较当前光标位置是否越界。 public Object next() { try { Object next = get(cursor); checkForComodification(); lastRet = cursor++; return next; } catch(IndexOutOfBoundsException e) { checkForComodification(); throw new NoSuchElementException(); } } /** * 在对一个集合对象进行跌代操作的同时,并不限制对集合对象的元素进行操作 * 这些操作包括一些可能引起跌代错误的add()或remove()等危险操作。 * 在AbstractList中,使用了一个简单的机制来规避这些风险。 * 这就是modCount和expectedModCount的作用所在 */ final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); }

优秀的个人博客,低调大师

打包报错 Error: java.util.concurrent.ExecutionException: com.android.build...

骚年,还记得当初为这个上刀山下火海,抓破脑袋都搞定不了的问题吗? 俗话说的好: 有问题找百度,教程都给你了 image.png 但是,这玩意儿是真的,百度出来不好使啊。 image.png 有无数个博客教你这么做吧? 不好意思过时了,2018年已经废除这个东西了。 其实AAPT2的错误并不复杂,90%以上都是你的资源文件出了问题,我们可以先尝试在app下面的build中加上两个配置属性: android{ //关闭AS校验我们的资源文件,或者说是PNG等图片 aaptOptions.cruncherEnabled = false aaptOptions.useNewCruncher = false 如果这样以来你发现你可以正常编译或者打包成功,那么肯定是你资源的问题,请好好排查。 gradle ar 这个命令也许会对你有帮助。 这两个属性建议仅仅是让你排查问题,希望不要把它当做解决方法而去使用,毕竟带着问题上线是件不好的事情;对吧。 具体的问题还是需要你看日志的,希望对你有所帮助。thanks。

优秀的个人博客,低调大师

Java---------- LeetCode——746. 使用最小花费爬楼梯

这个是最让我郁闷的我操,我用了一上午的时间来实现它,当然了刚开始我并没有用动态规划,我用自己的方式来实现,其实我不知道要用动态规划来实现它,写了一上午,最后发现,在LeetCode测试时,有些数据通不过啊,于是乎我就打补丁,打打补丁,最后发现实在打不下去补丁了,就于是乎用动态规划实现,但是我不会动态规划啊,那怎么办,学啊,我学了一会儿,感觉够用了, public static int minPay(int[] number) { // 我这个不怎么理解,现在理解了;原来这里多设置数组长度是为了fly(4)后边的做准备【表情】额 int[] fly = new int[number.length+1]; fly[0] = number[0]; fly[1] = number[1]; // 这个我也不怎么理解,这个number.length原来是为了达到数组长度时,加0直接输出啊。 for (int i = 2; i <=number.length ; i++) { int current = i == number.length ? 0 : number[i]; // 这个依然是核心代码,依然是看前边的怎么执行 fly[i] = Math.min(fly[i-1],fly[i-2])+current; } return fly[number.length]; }

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册
修改密码
忘记密码