Java parallel stream: how to wait for threads for a parallel stream to finish?

前端 未结 3 1069
梦谈多话
梦谈多话 2021-02-04 09:03

So I have a list from which I obtain a parallel stream to fill out a map, as follows:

Map map = new HashMap<>();
List

        
3条回答
  •  走了就别回头了
    2021-02-04 09:20

    Stream operations will block until done for both - parallel and not parallel implementations.

    So what you see is not the "putting data" process is still going on - most likely it's just data corruption, since HashMap is not threadsafe. Try using ConcurrentHashMap instead.

提交回复
热议问题