Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with Streams in Java 8

As per this article there are some serious flaws with Fork-Join architecture in Java. As per my understanding Streams in Java 8 make use of Fork-Join framework internally. We can easily turn a stream into parallel by using parallel() method. But when we submit a long running task to a parallel stream it blocks all the threads in the pool, check this. This kind of behaviour is not acceptable for real world applications.

My question is what are the various considerations that I should take into account before using these constructs in high-performance applications (e.g. equity analysis, stock market ticker etc.)

like image 878
akhil_mittal Avatar asked May 02 '26 15:05

akhil_mittal


1 Answers

The considerations are similar to other uses of multiple threads.

  • Only use multiple threads if you know they help. The aim is not to use every core you have, but to have a program which performs to your requirements.
  • Don't forget multi-threading comes with an overhead, and this overhead can exceed the value you get.
  • Multi-threading can experience large outliers. When you test performance you should not only look at throughput (which should be better) but the distribution of your latencies (which is often worse in extreme cases)
  • For low latency, switch between threads as little as possible. If you can do everything in one thread that may be a good option.
  • For low latency, you don't want to play nice, instead you want to minimise jitter by doing things such as pinning busy waiting threads to isolated cores. The more isolated cores you have the less junk cores you have to run things like thread pools.
like image 198
Peter Lawrey Avatar answered May 05 '26 03:05

Peter Lawrey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!