Multithreaded execution where order of finished Work Items is preserved

后端 未结 9 1188
无人共我
无人共我 2021-02-01 08:28

I have a flow of units of work, lets call them \"Work Items\" that are processed sequentially (for now). I\'d like to speed up processing by doing the work multithreaded.

<
9条回答
  •  独厮守ぢ
    2021-02-01 08:56

    If you allow BlockingQueue, why would you ignore the rest of the concurrency utils in java? You could use e.g. Stream (if you have java 1.8) for the above:

    List data = ...;
    List out = data.parallelStream()
        .map(t -> doSomeWork(t))
        .collect(Collectors.toList());
    

    Because you started from an ordered Collection (List), and collect also to a List, you will have results in the same order as the input.

提交回复
热议问题