在Java线程中对集合上的读写同时进行排序

时间:2019-07-11 04:10:58

标签: java multithreading thread-safety

我正在尝试一个代码,以查看如果一个线程修改一个集合而其他线程读取该集合会发生什么。这是代码

package threading;

import java.util.ArrayList;

public class ReadAndWrite {

    static ArrayList<Integer> coll = new ArrayList<Integer>();

    public static void main(String[] args) {
        coll.add(1);
        coll.add(3);
        coll.add(5);

    Thread t = new Thread() {
        public void run(){
            coll.add(2);
            coll.add(6);
            coll.add(8);
        }
    };
    Thread t1 = new Thread() {
            public void run(){
                System.out.println(" collection is "+coll + " and size is "+coll.size());
            }
    };
    Thread t2 = new Thread() {
        public void run(){
            System.out.println(" collection is "+coll+ " and size is "+coll.size());
        }
};
Thread t3 = new Thread() {
    public void run(){
        System.out.println(" collection is "+coll+ " and size is "+coll.size());
    }
};
Thread t4 = new Thread() {
    public void run(){
        System.out.println(" collection is "+coll+ " and size is "+coll.size());
    }
};
Thread t5 = new Thread() {
    public void run(){
        System.out.println(" collection is "+coll+ " and size is "+coll.size());
    }
};
t.start();

t1.start();

t2.start();
t3.start();

t4.start();
t5.start();

        }

        }

当我首先开始写线程时,这是我一直得到的响应。

collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5, 2, 6, 8] and size is 6

一旦我像下面这样更改顺序,事情就会变得混乱

 t1.start();
t.start();
t2.start();
t3.start();

t4.start();
t5.start();

响应

 collection is [1, 3, 5] and size is 3
 collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5] and size is 3
 collection is [1, 3, 5] and size is 3

如果我们进一步尝试订购,我们将看到如下错误

Exception in thread "Thread-1"  collection is [1, 3, 5, 2, 6, 8] and size is 6Exception in thread "Thread-2" 
 collection is [1, 3, 5, 2, 6, 8] and size is 6
 collection is [1, 3, 5, 2, 6, 8] and size is 6
java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
    at java.util.ArrayList$Itr.next(ArrayList.java:859)
    at java.util.AbstractCollection.toString(AbstractCollection.java:461)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at threading.ReadAndWrite$3.run(ReadAndWrite.java:28)
java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:909)
    at java.util.ArrayList$Itr.next(ArrayList.java:859)
    at java.util.AbstractCollection.toString(AbstractCollection.java:461)
    at java.lang.String.valueOf(String.java:2994)
    at java.lang.StringBuilder.append(StringBuilder.java:131)
    at threading.ReadAndWrite$2.run(ReadAndWrite.java:23)

问题:

  1. 在这里,同步或锁定似乎是一个显而易见的解决方案,但是为什么启动写入线程首先会给我们带来统一的结果,不是线程被认为是乱序的吗?
  2. 我认为同时写入是一个问题,但是即使只有一个写入线程,我们也会遇到错误,为什么?

在此先感谢您对上述情况的指导。席德

1 个答案:

答案 0 :(得分:0)

问题1的答案:关于线程应该无序运行的说法并不准确。几点: 1. Java没有指定如何实现线程,因此可以使用本机线程或绿色线程来实现,绿色线程是由JVM调度的Java唯一构造。线程的实现方式可能会影响运行时行为。 Linux和Windows上的JVM使用本机线程。 2.启动线程时,它会执行一段时间,然后中断,并运行另一个线程。您的线程做得很少,以至于当您运行写入器线程时,它会在计划其他线程之前运行完毕。

基于上述内容,您不能对线程何时运行以及它们将执行多少条语句做出任何假设,它取决于许多不同的变量,尤其是在使用由OS调度的本机线程时。

问题2的答案: 当您遍历ArrayList时,Iterator的next()方法将跟踪modCount。如果通过添加或删除元素来修改集合,则modCount将更改,并且与预期的modCount不匹配,因此Iterator将抛出ConcurrentModificationException。因此,即使只有1个编写器,如果您有并发阅读器,也可以获取ConcurrentModificationException

CopyOnWriteArrayList是线程安全的ArrayList实现