E
E
Evgeniy Rybalko2021-01-19 15:08:10
Java
Evgeniy Rybalko, 2021-01-19 15:08:10

Why are parallelStream threads in WAIT state?

Good afternoon,

I need to figure out why threads that are formed in parallelStream hang in the threaddump

"http-nio-8080-exec-147" #49010
  java.lang.Thread.State: WAITING
    at java.lang.Object.wait(Native Method)
    at java.util.concurrent.ForkJoinTask.externalAwaitDone(ForkJoinTask.java:334)
    at java.util.concurrent.ForkJoinTask.doInvoke(ForkJoinTask.java:405)
    at java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:734)
    at java.util.stream.ForEachOps$ForEachOp.evaluateParallel(ForEachOps.java:160)
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateParallel(ForEachOps.java:174)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:583)


Code example:

if (model != null) {
            model.parallelStream().forEach(this::handleSingleStep);
        }


The handleSingleStep method writes changes from the model to the MySQL database

As I understand it, it is a bad idea to parallelize the stream in order to write data to the database.
Please explain why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ryabos, 2021-01-23
@Ryabos

To accurately answer this question, you need to know what model is. If this is a stream obtained using Stream.iterate or limit was used, the JVM tries to parallelize execution, but as a result it only spawns streams

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question