Apa itu ConcurrentModificationException di Jawa?
Contoh
Mari kita lihat contoh bagaimana pengecualian ini terjadi.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);
}
}
}
}
Keluaran
Pengecualian di thread "utama" java.util.ConcurrentModificationException di java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:1042) di java.base/java.util.ArrayList$Itr.next(ArrayList.java :996) di tonggak sejarah.tiga puluh.tujuh.ConcurrentModificationExceptionTestDriver1.main(ConcurrentModificationExceptionTestDriver1.java:22)
Bagaimana Menghindari ConcurrentModificationException di Java?
Ada solusi berbeda untuk ConcurrentModificationException . Berikut ini beberapa di antaranya. Anda dapat memilih siapa pun sesuai kebutuhan Anda.- Hindari modifikasi saat iterasi.
- Gunakan loop tradisional untuk iterasi dan modifikasi secara bersamaan.
- Buat salinan nilai untuk diubah saat menggunakan iterator. Perbarui nilainya setelah iterasi selesai.
Contoh
Berikut ini contoh bagaimana Anda dapat menghindari 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);
}
}
Keluaran
Hari Kerja Awal: [Senin, Selasa, Rabu, Kamis, Jumat, Sabtu, Minggu] Hari Kerja Akhir: [Senin, Selasa, Rabu, Kamis, Jumat]
GO TO FULL VERSION