facebook

Solution Of Concurrent Exception In JAVA (Android)

By Soumendu Santra

Solution Of Concurrent Exception In JAVA (Android)

Hello guys! We all know about a Collection named ArrayList. You can find this class in java. This class belongs to the package “package java.util;”. We can store any kind of values inside this class and can sort, reverse accordingly. But you will face a problem while sorting with respect to any class with the help of comparator. In that case you are sorting with “Collections” class like “Collections.sort(myList,new Comparator)”. There might be an exception called “ConcurrentModificationException”, this happens because of the “Iterator” class. This “Iterator” class used to add or remove any data of the collection. Now, you have a question that you are not adding or removing any kind of data of that collection and you are not using “Iterator” class either then why are you going to face this exception? Yes! my friend the main problem is the method “sort()”, which belongs to the “Collections” class. In this method when you are sorting the collection the method converts the collection to “Array” then sort that collection, in this instance “Iterator” class is being used.

package java.util;

public static <T> void sort(List<T> list, Comparator<? super T> c) {

// BEGIN Android-changed: Compat behavior for apps targeting APIs <= 25.

// list.sort(c);

int targetSdkVersion = VMRuntime.getRuntime().getTargetSdkVersion();

if (targetSdkVersion > 25) {

list.sort(c);

} else {

// Compatibility behavior for API <= 25. http://b/33482884

if (list.getClass() == ArrayList.class) {

Arrays.sort((T[]) ((ArrayList) list).elementData, 0, list.size(), c);

return;

}

Object[] a = list.toArray();

Arrays.sort(a, (Comparator) c);

ListIterator<T> i = list.listIterator();

for (int j = 0; j < a.length; j++) {

i.next();

i.set((T) a[j]);

}

}

// END Android-changed: Compat behavior for apps targeting APIs <= 25.

}

So, now you need to handle this exception. But that is almost untouchable because it’s an inner class. So, how can you solve this? yes, you’re in right place now. There is another collection named “CopyOnWriteArrayList”, it belongs to package “package java.util.concurrent;”, this class also implements “List<>” like “ArrayList<>”. So, you won’t face any problem of casting. Now your question is what it does? what’s the difference? yes, there is a difference, this class has its own sort() method, which doesn’t use “Iterator” class. So, In this way, you can avoid “ConcurrentModificationException”. Your code will run smoothly.

package java.util.concurrent;

public void sort(Comparator<? super E> c) {

synchronized (lock) {

Object[] elements = getArray();

Object[] newElements = Arrays.copyOf(elements, elements.length);

@SuppressWarnings(“unchecked”) E[] es = (E[])newElements;

Arrays.sort(es, c);

setArray(newElements);

}

}

Android Developer , Openweb Solutions

Android Developer at Openweb Solutions

Posts created 2

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
shares