Scala's blocking context doesn't seem to do well with mixed blocking / non-blocking jobs. Why? Scala's blocking context doesn't seem to do well with mixed blocking / non-blocking jobs. Why? multithreading multithreading

Scala's blocking context doesn't seem to do well with mixed blocking / non-blocking jobs. Why?


blocking only takes effect once you've entered the blocking context. Since you have 8 non-blocking futures running, it won't start any new futures, so they can't enter the blocking context. In other words, Scala doesn't "know" they're blocking until they start being executed.

You can think of the second snippet as working like this:

  1. The first future is created and started.
  2. The first future signals that it is blocking via a call to blocking, so the implementation makes room for more futures.
  3. Meanwhile, on the main thread, the second future is created and started.
  4. ...

Whereas your last snippet works like this:

  1. The first future is created and started. It does not signal that it is blocking.
  2. The second future does the same.
  3. ...
  4. The 9th future is created, but not started, as there are 8 non-blocking futures.
  5. ...
  6. The 1001st future (the first one in the second loop) is created, but not started, as there are 8 non-blocking futures. Since it is not started, it never has the chance to tell the implementation that it is blocking by calling blocking.


Your first loop starts 8 threads, and blocks because it needs to start 992 more futures before completing.

Not sure what it is speicifcally, that "caught you off guard". Once the first foreach call completes, it'll move on to the second one, and start 26 more then.