欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

synchronizedList的线程不安全问题

程序员文章站 2022-06-07 13:19:57
...

我们知道ArrayList是线程不安全的,那么synchronizedList呢,有没有不安全的现象
试一试

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * @Auther: 罗罗
 * @Description:
 */
public class Test02 {

    public static void main(String[] args) {
        A a = new A();
        for (int i = 0; i < 10; i++) {
            new Thread(a).start();
        }
    }
}
class A implements Runnable{
    private static List list = Collections.synchronizedList(new ArrayList<String>());
    static {
        list.add("aa");
        list.add("bb");
        list.add("cc");
    }
    @Override
    public void run() {
         Iterator iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            list.add("655456");
        }
    }
}
“C:\Program Files\Java\jdk1.8.0_144\bin\java.exe" "-javaagent:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=62601:D:\IntelliJ IDEA\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;D:\IDEA2018\volatile\out\production\volatile" Test02
aa
Exception in thread "Thread-0" Exception in thread "Thread-2" Exception in thread "Thread-3" Exception in thread "Thread-1" Exception in thread "Thread-4" Exception in thread "Thread-6" Exception in thread "Thread-5" Exception in thread "Thread-7" Exception in thread "Thread-8" Exception in thread "Thread-9" java.util.ConcurrentModificationException
aa
aa
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
aa
aa
aa
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
aa
aa
	at A.run(Test02.java:34)
aa
aa
	at java.lang.Thread.run(Thread.java:748)
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at A.run(Test02.java:34)
	at java.lang.Thread.run(Thread.java:748)
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at A.run(Test02.java:34)
	at java.lang.Thread.run(Thread.java:748)
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at A.run(Test02.java:34)
	at java.lang.Thread.run(Thread.java:748)
java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at A.run(Test02.java:34)
	at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 0

发现报错,不是加了synchronized 关键字吗,看了一下源码,发现迭代器并没有加锁,并且推荐我们自己加锁

 public ListIterator<E> listIterator() {
            return list.listIterator(); // Must be manually synched by user
        }

        public ListIterator<E> listIterator(int index) {
            return list.listIterator(index); // Must be manually synched by user
        }

既然迭代器不行,那么使用增强for呢

for (String str : list) {
    System.out.print(str);
}

我们对增强for使用反编译

	Integer i;
    for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i)){
        i = (Integer)iterator.next();        
    }

发现也是使用了迭代器,增强for操作不是原子性的,如果在遍历的过程中,进行add,remove操作,就会抛出异常。
总结:使用SynchronizedList的时候,进行遍历时要手动进行同步处理 。