开发者

How to pass lists through the constructor in a thread-safe manner in Java?

开发者 https://www.devze.com 2023-04-04 10:42 出处:网络
I have a Java program that reads in a large list of strings from a file, creating lists of th开发者_运维问答em for every several hundred, and passes each list to a new thread that processes them in so

I have a Java program that reads in a large list of strings from a file, creating lists of th开发者_运维问答em for every several hundred, and passes each list to a new thread that processes them in some manner. This may include modifying the list. I want to know the best approach to accomplishing this so that the threads don't stomp on each other's lists. Note that I do not care about what kind of data structure this list actually is. It could be an array, a List, a queue, stack, etc. and order does not matter.

Thanks, Jared


If each thread has its own list, array, queue etc, there is no problem. If you use List.subList(), you must take a copy as this creates a view into the original list (which would cause a problem if you modified it in multiple threads).


Okay, how about ImmutableList (from Guava)? Each thread can subsequently copy it into its own thread-local mutable list, if it wants to.

If it really has to be mutable, a CopyOnWriteArrayList works too. :-)


If you're dealing with mutable data structures (for instance lists in which you can add / remove elements from) and you really need to share these data structures between threads, these would be my concerns:

  • At the very least you should synchronize all access to the data structure. This is best done by wrapping the data in a class, adding some

    final Object lock = new Object();
    

    and do synchronized (lock) { ... } around all relevant operations. A loop like

    while (!isEmpty())
        remove(0);
    

    would be a typical example which needs to be wrapped in such synchronized block.

  • You then need to make sure that the shared data structures is "logically synchronized" as well. If one thread for instance "adds a temporary object" which is not to be seen by the other methods, you need to lock (get exclusive right) to the data structure during this period of time. I.e. just wrapping add and remove operations in a synchronized block may not be enough.

  • If possible, use the data structures provided in the java.util.concurrent package. These things are truly tricky, and you would save yourself one or two headaches by leveraging the already debugged data structures in this package.

    Example classes from that package include CopyOnWriteArrayList and ConcurrentSkipListSet.


What I understand from your question is you are doing partition of a large data and using each partition in a new thread.

If this is correct, I don't see a problem since a partition is not shared among threads so no chance of stomping.

For partitioning, you can simply choose a limit and create a List/array out of that data and use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消