I have an arrayList which I am sharing between two threads and I am trying to iterate over and modify the list at the same time. I don't want to use the iterator's method, and I have also used the synchronized list but it still gives concurrentmodificationexception
.
code is as follows :
public class testing {
public static void main(String args[]){
ArrayList<String> al = new ArrayList<String>();
List<String> sal=Collections.synchronizedList(al);
String names[] = {"amol","Robin","vikas","shanu","mahesh"};
for(String x :names){
al.add(x);
}
Thread t1 = new Thread(new SyncArrayList(sal));
Thread t2 = new Thread(new SyncArrayList(sal));
t1.setName("T1");
t2.setName("T2");
t1.start();
t2.start();
}
}
class SyncArrayList implements Runnable
{
List<String> unsync ;
SyncArrayList(List<String> l){
this.unsync = l;
}
@Override
public void run() {
displayUnsyncList();
addNames();
}
void displayUnsyncList(){
synchronized(this){
ListIterator<String> itr = unsync.listIterator();
while(itr.hasNext()){
String x = itr.next();
System.out.println("Thread " +Thread.currentThread().getName() + " is displaying name : "+x);
}
}
}
void addNames(){
unsync.add("preet");
}
}
在多线程环境中,您应该考虑使用不会产生ConcurrentModificationException
CopyOnWriteArrayList
The javadoc for Collections.synchronizedList(...)
explains what you need to do:
void displayUnsyncList() {
synchronized (unsync) {
ListIterator<String> itr = unsync.listIterator();
while (itr.hasNext()) {
String x = itr.next();
System.out.println("Thread " +
Thread.currentThread().getName() +
" is displaying name : " + x);
}
}
In short, your code was synchronizing on the wrong object.
The iterator you are using is fail-fast iterator.Fail-fast iterator read internal data structure directly . The internal data structure should not be modified while iterating through the collection.
You should use Fail-safe iterator when one or more thread is iterating over the collection, in between, one thread changes the structure of the collection.
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
List<String> sal = Collections.synchronizedList(al);
String names[] = { "amol", "Robin", "vikas", "shanu", "mahesh" };
for (String x : names) {
al.add(x);
}
Thread t1 = new Thread(new SyncArrayList(sal));
Thread t2 = new Thread(new SyncArrayList(sal));
t1.setName("T1");
t2.setName("T2");
t1.start();
t2.start();
}
}
class SyncArrayList implements Runnable { List unsyn;
// List<String> unsync = Collections.synchronizedList(unsyn);
SyncArrayList(List<String> l) {
this.unsyn = new CopyOnWriteArrayList<String>(l);
}
@Override
public void run() {
displayUnsyncList();
addNames();
}
void displayUnsyncList() {
synchronized (this) {
Iterator<String> it = unsyn.iterator();
while (it.hasNext()) {
System.out.println("Thread " + Thread.currentThread().getName() + " is displaying name : " + it.next());
}
}
}
void addNames() {
unsyn.add("preet");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.