3

I have a Stream as a method's argument. I don't know if it parallel or not. How can I guarantee sequential execute it?

(no if/else/instance of pls)

Thanks

Thom
  • 14,013
  • 25
  • 105
  • 185
arminvanbuuren
  • 957
  • 1
  • 9
  • 16
  • 9
    You can simply use `sequential()`. – MC Emperor Sep 03 '18 at 18:32
  • @nullpointer throw me the link in comments here (I'll take a look), I can still vote to close... – Eugene Sep 03 '18 at 18:37
  • 3
    https://stackoverflow.com/questions/33835683/how-to-restrict-a-stream-to-run-sequentially-and-prevent-it-from-running-in-par ; https://stackoverflow.com/questions/29216588/how-to-ensure-order-of-processing-in-java8-streams ; https://stackoverflow.com/questions/35742640/calling-sequential-on-parallel-stream-makes-all-previous-operations-sequential @Eugene – Naman Sep 03 '18 at 18:40

1 Answers1

9

wait... just invoke sequential() on it?

This is just a flag so the last invocation of sequential/parallel wins, that means even if your stream is parallel and you receive it as a method argument, just invoke sequential anywhere before the terminal operation and you are done

Eugene
  • 117,005
  • 15
  • 201
  • 306