About active failure, rapid-fail, fail safe

1, active failure


Concurrent multi-linear, if A thread modifies shared variables, then thread B is not aware of this change shared variable, called active failure.

How to solve the activity fails, it would need variables this happens-before relationship between two threads, the most common is volatile or locked.

 

2, quick failure (fail-fast)


When the collection to iterate the collection if there are other threads add and delete operations, iterative error quickly.

ConcurrentModificationException throws an exception, called fail-fast.

Example: (HashMap Similarly)

import java.util.ArrayList;
import java.util.List;

class YfModel{
    private String name;

    public YfModel(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class ExceptionYf {
    public static void main(String[] args){
        YfModel yfModel = new YfModel("default1");
        YfModel yfMode2 = new YfModel("default2");
        YfModel yfMode3 = new YfModel("default3");
        YfModel yfMode4 = new YfModel("default4");
        YfModel yfMode5 = new YfModel("default5");
        List<YfModel> yfModelList = new ArrayList<>();
        yfModelList.add(yfModel);
        yfModelList.add(yfMode2);
        yfModelList.add(yfMode3);
        yfModelList.add(yfMode4);

        for(YfModel tempYfModel : yfModelList){
            System.out.println("valueModel="+ tempYfModel.getName());
            yfModelList.add(yfMode5);
        }

    }
}
View Code

Error:

Exception in thread "main" java.util.ConcurrentModificationException
 at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
 at java.util.ArrayList$Itr.next(ArrayList.java:859)
 at com.yangfei.test.exceptiontest.ExceptionYf.main(ExceptionYf.java:36)

 

note:

Quick failure is only the number of elements to detect whether there is a change of the set, if the element is an object, then modify the object. It does not complain.

 

 

3, security failure (fail-safe)


When iteration of the collection a copy of the original collection of copies of the new elements to iterate, called security failures

Such modifications of the original collection during the iteration will not perceive. java concurrent collections framework (java.util.concurrent) iterations are safe failure

Example:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class YfModel{
    private String name;

    public YfModel(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
public class ExceptionYf {
    public static void main(String[] args){
        YfModel yfModel = new YfModel("default1");
        YfModel yfMode2 = new YfModel("default2");
        YfModel yfMode3 = new YfModel("default3");
        YfModel yfMode4 = new YfModel("default4");
        YfModel yfMode5 = new YfModel("default5");
        List<YfModel> yfModelList = new ArrayList<>();
        yfModelList.add(yfModel);
        yfModelList.add(yfMode2);
        yfModelList.add(yfMode3);
        yfModelList.add(yfMode4);

        Map<String,YfModel> yfMap = new ConcurrentHashMap<>();
        yfMap.put("default1",yfModel);
        yfMap.put("default2",yfMode2);
        yfMap.put("default3",yfMode3);
        yfMap.put("default4",yfMode4);

        for(Map.Entry<String, YfModel>  entry : yfMap.entrySet()){
            System.out.println("valueModel="+ entry.getValue().getName());
            yfMap.put("default5",yfMode5);
        }
    }
}
View Code

Output:

valueModel=default3
valueModel=default4
valueModel=default1
valueModel=default2

 

The new elements of the iterative process and not output. Even if modify the attributes of the object element, and it is imperceptible.

Guess you like

Origin www.cnblogs.com/yangfei629/p/11456534.html