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
  • 12,633
  • 23
  • 97
  • 164
arminvanbuuren
  • 727
  • 1
  • 6
  • 14
  • 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
  • 110,516
  • 12
  • 173
  • 277