Search code examples
javalistmultithreadingcollectionsconcurrency

Are there any CopyOnRead lists or other implementations in Java?


Write operations more than read operations, what is the implementation? I searched a lot, but nothing came up. I need a list collection that supports concurrent programming.

My program: Netty is used, and there is a list to store connections that are not logged in. The user is added to the list when the connection is successful. The user is deleted from the list after successful login. Unlogged connections are cleared every 30 seconds.

So I need a list with more write operations than read operations.


Solution

  • Copying on read1 would not be a concurrency optimization2. Thus, there are no concurrent copy-on-read implementations of List.

    A number of concurrent implementations of Queue or Deque exist, in both blocking and non-blocking forms.

    However, if your application really requires a List (rather than a Queue or Deque), your best option is to use a mutex to synchronize ALL access and update operations on the list. It is not highly concurrent, but c'est la vie!

    Looking at your use-case, it seems to me that you actually need a concurrent set or map rather than a list. Your comments confirm this.


    1 - Copy-on-read is sometimes used to refer to the practice of returning a copy of an internal data structure via an API. This is orthogonal to concurrency / thread-safety.
    2 - As far as I am aware ...