Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
parallelStream for time consuming methods

I have something like this:

// sequential version
for(var t: tasks){ 
    // run() is a time consuming method (approx. 20-30 seconds)
    t.run();
}

I have approx. 1000 (independent) tasks and the above needs approx. 1000 x 25 seconds. The run()-method is only CPU intensive (there are no I/O Operations involved).

I switched to parallelStream:

// parallel version
tasks.parallelStream().forEach(Task:run);

and measured that with parallelStream each task.run() needs approx. 15-20 seconds. So it's definitely a little faster than using the sequential version.

Can I use parallelStream (with its fork-join) for such long running operations (each task.run() needs approx. 15-20 seconds) or would it be better to use executorServices with custom thread pools (for example, because of system stability, performance, etc.)?

What I want to know if there is any harm when using parallelStream (fork-join) for such long running tasks. I have thought that fork-join should only be used for very short running tasks (max. 1-2 seconds), but I am not sure if so and why.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Collection#parallelStream is simpler - you need less code, but you have no control over when, or even if, parallelism is used.

ExecutorService gives you a lot of control over parallel processing, but exposes more of the workings, so you need more code and more knowledge (but not a lot of either).

Try parallel stream first. If that seems good enough and you’re not processing many other requests, stop there.

If parallelism seems not be to sufficient, or if you are processing other requests, use executor service, because all streams share a single thread pool, so if they are kept busy, other stream based code will freeze.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda