ConcurrentModificationException产生原因的情况解析

本文主要是介绍ConcurrentModificationException产生原因的情况解析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


产生这个异常,归根结底的原因是因为,一个list或者集合 在遍历的同时进行了 写或者删除操作,改变了 list的大小:

    private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;


        public boolean hasNext() {
            return cursor != size;
        } 

@SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }


        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();


            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }


        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }


 }

造成这个现象的原因:

1 简单直接的原因,遍历的时候进行了增 删操作

for (String str : list) {

list.add(s); 

or 

list.remove(0);

}

2. 还有一种情况 就是一个list 在多线程环境先,被不同的线程在遍历的同时,进行了增删操作

/**
 * 中文转汉语拼音
 * 支持多音字
 */
public class HanyuPinyinHelper {


    private static StringBuffer buffer;
    private static List<String> list;
    private static Properties p;
    private static boolean isSimple;


    static{
        buffer = new StringBuffer();
        list = new ArrayList<String>();
        p = new Properties();
        isSimple=false;
        try {
            p.load(new BufferedInputStream(HanyuPinyinHelper.class
                    .getResourceAsStream("/hanyu_pinyin.txt")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    public static String[] getHanyuPinyins(char c) {
        int codePointOfChar = c;
        String codepointHexStr = Integer.toHexString(codePointOfChar)
                .toUpperCase();
        String str = (String) p.get(codepointHexStr);
        return str.split(",");
    }


    /**
     * @param str 需要转换的字符串
     * @param isSimple true简拼,false全拼
     * @return 该字符串转换后的所有组合
     */
    public static List<String> hanyuPinYinConvert(String str,boolean isSimpleFlag) {
        if (str == null || "".equals(str))
            return null;
        isSimple=isSimpleFlag;
        list.clear();
        buffer.delete(0, buffer.length());
        convert(0, str);
        return list;
    }




    /**
     * @param str 需要转换的字符串
     * @return 该字符串转换后的所有组合,包含全拼和简拼
     */
    public static List<String> hanyuAllPinYinConvert(String str) {
        if (str == null || "".equals(str))
            return null;
        list.clear();
        buffer.delete(0, buffer.length());
        isSimple=true;
        convert(0, str);
        buffer.delete(0, buffer.length());
        isSimple=false;
        convert(0, str);
        return list;
    }


    private static void convert(int n, String str) {
        if (n == str.length()) {// 递归出口
            String temp=buffer.toString();
            if(!list.contains(temp)){
                list.add(buffer.toString());
            }
            return;
        } else {
            char c = str.charAt(n);
            if (0x3007 == c || (0x4E00 <= c && c <= 0x9FA5)) {// 如果该字符在中文UNICODE范围
                String[] arrayStrings = getHanyuPinyins(c);
                if (arrayStrings == null) {
                    buffer.append(c);
                    convert(n + 1, str);
                } else if (arrayStrings.length == 0) {
                    buffer.append(c);
                    convert(n + 1, str);
                } else if (arrayStrings.length == 1) {
                    if(isSimple){
                        if(!"".equals(arrayStrings[0])){
                            buffer.append(arrayStrings[0].charAt(0));
                        }
                    }else{
                        buffer.append(arrayStrings[0]);
                    }
                    convert(n + 1, str);
                } else {
                    int len;
                    for (int i = 0; i < arrayStrings.length; i++) {
                        len = buffer.length();
                        if(isSimple){
                            if(!"".equals(arrayStrings[i])){
                                buffer.append(arrayStrings[i].charAt(0));
                            }
                        }else{
                            buffer.append(arrayStrings[i]);
                        }
                        convert(n + 1, str);
                        buffer.delete(len, buffer.length());
                    }
                }
            } else {// 非中文
                buffer.append(c);
                convert(n + 1, str);
            }
        }
    }


    public static void main(String[] args) {
        List list1 = HanyuPinyinHelper.hanyuAllPinYinConvert("瞿乐底");
        System.out.println(list1);


    }


}


这篇关于ConcurrentModificationException产生原因的情况解析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/605432

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

python常见环境管理工具超全解析

《python常见环境管理工具超全解析》在Python开发中,管理多个项目及其依赖项通常是一个挑战,下面:本文主要介绍python常见环境管理工具的相关资料,文中通过代码介绍的非常详细,需要的朋友... 目录1. conda2. pip3. uvuv 工具自动创建和管理环境的特点4. setup.py5.

全面解析HTML5中Checkbox标签

《全面解析HTML5中Checkbox标签》Checkbox是HTML5中非常重要的表单元素之一,通过合理使用其属性和样式自定义方法,可以为用户提供丰富多样的交互体验,这篇文章给大家介绍HTML5中C... 在html5中,Checkbox(复选框)是一种常用的表单元素,允许用户在一组选项中选择多个项目。本

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Redis过期删除机制与内存淘汰策略的解析指南

《Redis过期删除机制与内存淘汰策略的解析指南》在使用Redis构建缓存系统时,很多开发者只设置了EXPIRE但却忽略了背后Redis的过期删除机制与内存淘汰策略,下面小编就来和大家详细介绍一下... 目录1、简述2、Redis http://www.chinasem.cn的过期删除策略(Key Expir