Apa ConcurrentModificationException ing Jawa?

"The ConcurrentModificationException occurs nalika sumber daya diowahi nalika ora duwe hak istimewa kanggo modifikasi."
Ing tembung liyane, Java ConcurrentModificationException (minangka jeneng kasebut) kedadeyan amarga masalah konkurensi. Dianggep sampeyan duwe kawruh sadurunge babagan utas lan multi-threading sadurunge nerusake. Sawetara kelas ing Jawa, kaya kelas Koleksi, ora ngidini thread kanggo ngowahi unsur nalika thread liyane wis iterating liwat. Yen operasi modifikasi (nambah, mbusak etc) dileksanakake, program mbalang ConcurrentModificationException . Iki ora mung kasus pangecualian iki kedadeyan. Nanging, ngeduk luwih jero bakal metu saka ruang lingkup artikel iki.

Tuladha

Ayo goleki conto carane pengecualian iki kedadeyan.
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;

//Test driver for ConcurrentModificationException
public class ConcurrentModificationExceptionTestDriver1 {

	public static void main(String[] args) {
		List<String> businessDays = new ArrayList<>();

		businessDays.add("Monday");
		businessDays.add("Tuesday");
		businessDays.add("Wednesday");
		businessDays.add("Thursday");
		businessDays.add("Friday");
		businessDays.add("Saturday");
		businessDays.add("Sunday");

		Iterator<String> iterator = businessDays.iterator();
		while (iterator.hasNext()) {
			String day = iterator.next();
			if (day.equals("Sunday")) {
				// removing a list item while iterating
				// leading to java.util.ConcurrentModificationException
				businessDays.remove(day);
			}
		}
	}
}

Output

Pangecualian ing thread "utama" java.util.ConcurrentModificationException ing java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) ing java.base/java.util.ArrayList$Itr.next(ArrayList.java :996) ing milestone.thirty.seven.ConcurrentModificationExceptionTestDriver1.main(ConcurrentModificationExceptionTestDriver1.java:22)

Kepiye Cara Ngindhari ConcurrentModificationException ing Jawa?

Ana macem-macem solusi kanggo ConcurrentModificationException . Ing kene ana sawetara. Sampeyan bisa milih sapa wae miturut kabutuhan sampeyan.
  1. Aja modifikasi nalika iterasi.
  2. Gunakake puteran tradisional kanggo pengulangan lan modifikasi simultan.
  3. Gawe salinan nilai sing bakal diowahi nalika nggunakake iterator. Nganyari nilai yen pengulangan wis rampung.

Tuladha

Punika conto carane sampeyan bisa ngindhari ConcurrentModificationException .
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//Test driver for ConcurrentModificationException
public class ConcurrentModificationExceptionTestDriver2 {

	public static void main(String[] args) {

		List<String> businessDays = new ArrayList<>();

		businessDays.add("Monday");
		businessDays.add("Tuesday");
		businessDays.add("Wednesday");
		businessDays.add("Thursday");
		businessDays.add("Friday");
		businessDays.add("Saturday");
		businessDays.add("Sunday");

		System.out.println("Initial Business Days: " + businessDays);

		// Method II - Using traditional for loop
		for (int i = 0; i < businessDays.size(); i++) {
			if (businessDays.get(i).equals("Sunday")) {
				businessDays.remove(i);
			}
		}

		Iterator<String> iterator = businessDays.iterator();
		List<String> businessDaysToBeRemoved = new ArrayList<>();

		// Method III - Creating a copy of elements
		while (iterator.hasNext()) {
			String day = iterator.next();
			if (day.equals("Saturday")) {
				// Add values to be removed in the list
				businessDaysToBeRemoved.add(day);
			}
		}
		businessDays.removeAll(businessDaysToBeRemoved);
		System.out.println("Final Business Days: " + businessDays);
	}
}

Output

Dina Kerja Awal: [Senin, Selasa, Rebo, Kamis, Jum'at, Setu, Minggu] Dina Kerja Akhir: [Senin, Selasa, Rebo, Kemis, Jum'at]

Kesimpulan

Iki minangka dosis cepet kanggo ngatasi ConcurrentModificationException ing Jawa. Bebas bae kanggo mlumpat nalika sampeyan aran macet. Kaya biasane, kunci kanggo maju yaiku latihan lan sabar. Nganti saiki, terus sinau lan terus berkembang.