【问题标题】:Ordering in simultaneous Read and Write on a collection in Threads in Java在 Java 中的线程中对集合进行同时读取和写入的排序
【发布时间】:2019-07-11 04:10:58
【问题描述】:

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

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. 我认为同时写入是一个问题,但即使我们只有一个写入线程,我们也会遇到错误,为什么?

将非常感谢有关上述方案的指导,在此先感谢。席德

【问题讨论】:

    标签: java multithreading thread-safety


    【解决方案1】:

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

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

    问题 2 的答案: 当您在 ArrayList 上进行迭代时,Iterator 的 next() 方法会跟踪 modCount。如果您通过添加或删除元素来修改集合,则 modCount 将发生变化,并且与预期的 modCount 不匹配,因此 Iterator 将抛出 ConcurrentModificationException。因此,即使您只有 1 个写入器,如果您有一个并发读取器,您也可以获得 ConcurrentModificationException

    CopyOnWriteArrayList 是线程安全的 ArrayList 实现

    【讨论】:

      猜你喜欢
      • 2011-04-06
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-20
      • 2011-03-19
      • 2021-03-04
      相关资源
      最近更新 更多