nus-cs2030 / 2021-s2

2 stars 2 forks source link

CompletableFuture anyOf #371

Open Ryanng-zr opened 3 years ago

Ryanng-zr commented 3 years ago

Hi guys, I am aware that anyOf returns the first value that is completed, however what if all the CompletableFutures complete at the same time? Which value will be returned in this case?

image

kexinnnwang commented 3 years ago

I believe anyOf would come in handy if you have method chains after it. After any of the cfs is completed, the next method can then run (which is different from allOf, which requires all to be completed). I dont think the order will be fixed because just like allOf, the order changes each time you run it

clementyee303 commented 3 years ago

I'm guessing the choice of return would be arbitrary if both were to be completed at the exact same time, much like a comparator returning 0 for a sort - the order is chosen randomly

Brandon-Chiu commented 3 years ago

Hi, I think that the value which will be returned will not be fixed because the first returned value can vary.

JWulaXia commented 3 years ago

i think if all finishes on the same time then maybe the computer then randomly choose one to proceed to the next round. this anyof is most handy when you just need one value to proceed to the next stage