Java 8 stream combiner never called

前端 未结 2 1786
误落风尘
误落风尘 2021-02-05 04:25

I\'m writing a custom java 8 collector which is supposed to compute the average of a POJO which has a getValue() method. Here\'s the code:

public st         


        
2条回答
  •  情歌与酒
    2021-02-05 05:16

    It looks like the problem is the CONCURRENT characteristic, which does something else than you would think it might:

    Indicates that this collector is concurrent, meaning that the result container can support the accumulator function being called concurrently with the same result container from multiple threads.

    Instead of calling the combiner, the accumulator is being called concurrently, using the same BigDecimal[] a for all threads. The access to a is not atomic, so it goes wrong:

    Thread1 -> retrieves value of a[0]: 3
    Thread2 -> retrieves value of a[0]: 3
    Thread1 -> adds own value: 3 + 3 = 6
    Thread2 -> adds own value: 3 + 4 = 7
    Thread1 -> writes 6 to a[0]
    Thread2 -> writes 7 to a[0]
    

    Making the value of a[0] 7 when it should be 10. The same kind of thing can happen with a[1], so results can be inconsistent.


    If you remove the CONCURRENT characteristic, the combiner will get used instead.

提交回复
热议问题