Java中的fail-fast機制使用詳解
Java的fail-fast機制使用
fail-fast 機制是Java集合(Collection)中的一種錯誤機制。當多個線程對同一個集合的內容進行操作時,就可能會產(chǎn)生fail-fast事件。
例如:
- 當某一個線程 A 通過 iterator 去遍歷某集合的過程中,若該集合的內容被其他線程所改變了,那么線程 A 訪問集合時,就會拋出 ConcurrentModificationException 異常,產(chǎn)生 fail-fast 事件。
- 這里的操作主要是指 add、remove 和 clear,對集合元素個數(shù)進行修改。
舉例代碼
單線程,在foreach循環(huán)里對某些集合元素進行元素的remove/add操作的時候,會觸發(fā)fail-fast機制
public static void main(String[] args){
List<String> strList = new ArrayList<>();
strList.add("AA");
strList.add("aa");
strList.add("BB");
strList.add("CC");
for(String str : strList){
if("aa".equals(str)){
strList.remove(str);
}
}
}多線程,在一個線程讀時,另一個線程寫入list,讀線程會fail-fast
// 測試
public class TreadDemo1 {
public static void main(String[] args){
List<String> strList = new ArrayList<>();
strList.add("AA");
strList.add("aa");
strList.add("BB");
strList.add("CC");
strList.add("DD");
new MyThread1(strList).start();
new MyThread2(strList).start();
}
static class Mythread1 extends Thread {
private List<String> list;
public Mythread1(List<String> list){
this.list = list;
}
@Override
public void run(){
for(String str : list){
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println("MtThread1:"+str);
}
}
}
static class MyThread2 extends Thread {
private list<String> list;
public Mythread2(List<String> list){
this.list = list;
}
@Override
public void run(){
for(int i = 0; i < list.size(); i++){
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
if("aa".equals(list.get(i))){
list.remove(i);
}
}
System.out.println("MtThread2:"+list);
}
}
}原理
將單線程編譯后的.class反編譯后發(fā)現(xiàn),foreach其實是依賴while循環(huán)和Iterator實現(xiàn)的。
通過跟蹤代碼的異常堆棧,發(fā)現(xiàn)真正拋出異常的代碼是:java.util.ArrayList$Itr.checkForComodification();該方法實在iterator.next()方法中調用的:
final void checkForComodification(){
if(modCount != expectedModCount)
throw new ConcurrentModificationException();
}在該方法中modCount 和 expectedModCount進行了比較,如果二者不相等,則拋出ConcurrentModificationException 異常。
modCount是ArrayList中的一個成員變量。表示該集合實際被修改的次數(shù)。(操作集合類的remove()、add()、clear()方法會改變這個變量值)expectedModCount是ArrayList 中的一個內部類---Itr(Iterator接口)中的成員變量。表示這個迭代器預期該集合被修改的次數(shù)。其值隨著Itr被創(chuàng)建而初始化。只有通過迭代器對集合進行操作,該值才會改變。
所以,在使用Java的集合類的時候,如果發(fā)生ConcurrentModificationException 異常,優(yōu)先考慮fail-fast有關的情況。
解決方式
1)使用普通for循環(huán)進行操作
普通for循環(huán)沒有使用到Iterator的遍歷,所以不會進行fail-fast的檢驗。
public static void main(String[] args){
List<String> strList = new ArrayList<>();
strList.add("AA");
strList.add("aa");
strList.add("BB");
strList.add("CC");
strList.add("DD");
for(int i = 0; i < strList.size(); i++){
if("aa".equals(strList.get(i))){
strList.remove(i);
}
}
}2)直接使用Iterator 進行操作
public static void main(String[] args){
List<String> strList = new ArrayList<>();
strList.add("AA");
strList.add("aa");
strList.add("BB");
strList.add("CC");
strList.add("DD");
Iterator<String> iterator = strList.iterator();
while(iterator.hasNext()){
if("aa".equals(iterator.next())){
iterator.remove();
}
}
}3)使用Java 8中提供的filter 過濾
Java 8 中可以把集合轉換成流,對于流有一種filter操作,可以對原始Stream 進行某項過濾,通過過濾的元素被留下了生成一個新的Stream。
public static void main(String[] args){
List<String> strList = new ArrayList<>();
strList.add("AA");
strList.add("aa");
strList.add("BB");
strList.add("CC");
strList.add("DD");
strList = strList.stream().filter(e -> !"aa".equals(e)).collect(Collectors.toList());
System.out.println(strList);
}4)使用fail-safe的集合類
為了避免觸發(fā)fail-fast機制導致異常,我們可以使用Java中提供的一些采用了fail-safe機制的集合類。
java.util.concurrent包下的容器都是fail-safe的,可以在多線程下并發(fā)使用,并發(fā)修改。同時也可以在foreach中進行add/remove等操作。
5)也可以使用foreach循環(huán)
如果我們非常確定一個集合中,某個即將刪除的元素只包含一個的話,也是可以使用foreach循環(huán)的,只要刪除之后,立即結束循環(huán)體,不在繼續(xù)執(zhí)行遍歷就可以。
public static void main(String[] args){
List<String> strList = new ArrayList<>();
strList.add("AA");
strList.add("aa");
strList.add("BB");
strList.add("CC");
for(String str : strList){
if("aa".equals(str)){
strList.remove(str);
break;
}
}
System.out.println(strList);
}總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot部署到外部Tomcat無法注冊到Nacos服務端的解決思路
這篇文章主要介紹了SpringBoot部署到外部Tomcat無法注冊到Nacos服務端,本文給大家分享完美解決思路,結合實例代碼給大家講解的非常詳細,需要的朋友可以參考下2023-03-03
詳解Java中格式化日期的DateFormat與SimpleDateFormat類
DateFormat其本身是一個抽象類,SimpleDateFormat 類是DateFormat類的子類,一般情況下來講DateFormat類很少會直接使用,而都使用SimpleDateFormat類完成,下面我們具體來看一下兩個類的用法:2016-05-05
深入解析Jdk8中Stream流的使用讓你脫離for循環(huán)
這篇文章主要介紹了Jdk8中Stream流的使用,讓你脫離for循環(huán),本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

