fail-fast 机制
Posted 技术无产者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了fail-fast 机制相关的知识,希望对你有一定的参考价值。
在项目中,写的一段程序为对Set集合遍历的过程中异常set集合里的元素,导致出现下面的异常:
Set<String> keySet=disposableBeansMap.keySet ();
keySet.toArray ();
for (String key:keySet)
{
try {
//从单例缓存中清除bean
singletonObjectMap.remove (key);
//从销毁缓存中取出Bean对应的销毁函数
DisposableBean disposable = disposableBeansMap.remove (key);
disposable.destory ();
} catch (Exception e) {
throw new BeanException ("Destory method on bean with name ' "+key+ " ' throw an exception:",e);
}
}
Exception in thread "destoryThread" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
at java.util.HashMap$KeyIterator.next(HashMap.java:1469)
at com.beanbox.beans.registry.support.DefaultSingletonBeanRegistry.destorySingletons(DefaultSingletonBeanRegistry.java:61)
at com.beanbox.context.AbstractApplicationContext.close(AbstractApplicationContext.java:91)
at java.lang.Thread.run(Thread.java:748)
解决办法:
Set<String> keySet=disposableBeansMap.keySet ();
//解决fail-fast
String[] beanNames = keySet.toArray (new String[0]);
for (String key:beanNames)
{
try {
//从单例缓存中清除bean
singletonObjectMap.remove (key);
//从销毁缓存中取出Bean对应的销毁函数
DisposableBean disposable = disposableBeansMap.remove (key);
disposable.destory ();
} catch (Exception e) {
throw new BeanException ("Destory method on bean with name ' "+key+ " ' throw an exception:",e);
}
}
详细介绍:
以上是关于fail-fast 机制的主要内容,如果未能解决你的问题,请参考以下文章