简体   繁体   English

是否可以允许不同的线程同时读取和写入同一集合?

[英]Is it possible to allow different thread both read to and write from the same collection?

I have the following wrapper: 我有以下包装:

public UserContianer{

    private List<User> users;


    public void add(User u){
         users.add(u)
    }

    public void update(User u){
        if(users.contains(u))
             //updating with the new value u
    }

    public User get(int index){
        return users.get(index);
    }
}

I need to allow multiple thread to read from and write the container simultaneously such that 我需要允许多个线程同时读取和写入容器,以便

  • if read and written element of the list are different- that should be ok 如果列表的读写元素不同-应该可以
  • if read and written element of the list are the same then 如果列表的读写元素相同,则

    • if there's already a thread updating the element all read operation should be blocked untill the write operation's finished 如果已经有一个线程在更新元素,则所有读操作都应被阻止,直到写操作完成

    • if there's a thread reading some element, all write operations should be blocked until the reading's finished 如果有一个线程正在读取某些元素,则应阻止所有写入操作,直到读取完成

First of all, are such requirements even make sense? 首先,这样的要求是否有意义? I'm new to multithreading and maybe that's actualy not what I wanted. 我是多线程的新手,也许实际上不是我想要的。

Secondly, is it possible to implement the requirements in Java? 其次,可以用Java实现这些要求吗?

One option is to wrap your existing List in a synchronized version with Collections.synchronizedList(List<T> list) . 一种选择是使用Collections.synchronizedList(List<T> list)将您现有的List包装在一个同步版本中。 This allows thread-safe use of the List, if all access is through the synchronized wrapper. 如果所有访问都是通过同步包装器进行的,则允许线程安全使用List。

Note that the elements in the List are references . 请注意,列表中的元素是引用 If you want to lock the List while you're making changes to objects, you'll need to synchronize at a higher level. 如果要在更改对象时锁定列表,则需要在更高级别进行同步。 One way to do this is to mark methods as synchronized . 一种方法是将方法标记为已synchronized

 public synchronized void update(User u) { ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有可能从不同 class 的同一端口上的套接字写入/读取 - Is there a possiblity to write/read from a socket on same port from different class 如果2个不同的写入和读取线程永远不会同时存在,我是否需要使用volatile - Do I need to use volatile, if 2 different write and read thread will never alive at the same time 如何确保写入发生在同一存储桶和同一键上的不同线程同时读取并发哈希映射之前? - How can I make sure that write happens before read in concurrenthashmap at the same time by different thread at same bucket and on a same key? 是否可以从同一个 xlsx 文件中的不同工作表读取数据? - Is it possible to read the data from different sheets in a same xlsx file? 从同一表读取两个线程:如何使两个线程不从TASKS表读取同一组数据 - Two threads reading from the same table:how do i make both thread not to read the same set of data from the TASKS table 是否可以在同一xls表上进行读写? - Is this possible to read and write on same xls sheet? 是否可以同时读写文件? - Is it possible to read and write in file at the same time? 服务器端套接字是否可以对文件进行读写操作? - Is it possible for a server side socket to do both read and write operations on files? 线程不能同时在同一个套接字上读写流? - Thread cannot read and write streams on the same socket at the same time? 在同一字段上读取和写入的不同@JsonProperty 配置? - Different @JsonProperty config for READ and WRITE on same field?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM