• Register
Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

When concurrentmodificationexception is thrown in java ?

0 votes
875 views
When concurrentmodificationexception is thrown in java ?
asked Jan 28, 2014 in Java by Akshit Gupta

1 Answer

0 votes

When you run the following example then you got ConcurrentModificationException:

Main File: (ConcurrentModificationException.java)

package test.com.collection;

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

public class ConcurrentModificationException {

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<Employee>();
        Employee emp;
        emp = new Employee(1, "Rohit", "Agrawal", "EMP001", 54000D);
        employees.add(emp);
        emp = new Employee(2, "Pin", "Rai", "EMP002", 1400D);
        employees.add(emp);
        emp = new Employee(3, "Sohan", "Kumar", "EMP003", 5000D);
        employees.add(emp);
        emp = new Employee(4, "Suresh", "Dhull", "EMP004", 24700D);
        employees.add(emp);
        
        doModifyList(employees);
    }
    
    private static void doModifyList(List<Employee> employees) {
        int index = 0;
        for(Employee emp: employees){
            System.out.println(emp);
            if(emp.getFirstName().equals("Pin")){
                employees.add(index, new Employee(4, "SureshNew", "DhullNew", "EMP004", 24700D));
                employees.remove(emp);
            }
            index ++;
        }
    }

}

Bean class:- (Employee.java)

package test.com.collection;

public class Employee {
    private Integer id;
    private String firstName;
    private String lastName;
    private String code;
    private Double salary;
    
    public Employee(){
        
    }
    
    public Employee(Integer id, String firstName, String lastName, String code, Double salary){
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.code = code;
        this.salary = salary;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }
    
    @Override
    public String toString() {
        return "Id: "+ id +" FirstName: " + firstName + " LastName: " + lastName + " Code: " + code +" Salary: "+ salary;
    }
    
}


Output:
Id: 1 FirstName: Rohit LastName: Agrawal Code: EMP001 Salary: 54000.0
Exception in thread "main" Id: 2 FirstName: Pin LastName: Rai Code: EMP002 Salary: 1400.0
java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at test.com.collection.ConcurrentModificationException.doModifyList(ConcurrentModificationException.java:25)
    at test.com.collection.ConcurrentModificationException.main(ConcurrentModificationException.java:20)
 

answered Jan 28, 2014 by Naren saini
...