1

I have a function to model my Data. If the parameter "Entity" contains child, it is enclosed in child names. There are two types of child Type-A & Type-B. For Each type the function is called recursively. This recursive function call exits when the child do not have any more child names.

public void modelMyData(Entity entity) {

    if (entity.getChildNames()[0] != null) {
        Arrays.stream(entity.getChildNames())
                .collect(Collectors.toList())
                .parallelStream()
                .forEach(childType -> {

                        entity.getChild(childType).parallelStream()
                                .forEach(child -> {
                                    modelMyData(child);

                                    });
                        ;
                    });
    }

    System.out.println("INSERT " + entity.getChildAttributeValue());

}

The program works fine for me. But the use of parallel stream is said to be bad in java programming.

Visit http://zeroturnaround.com/rebellabs/java-parallel-streams-are-bad-for-your-health/

Should I use streams instead of parallel streams?

4
  • make performance test to see if parallelStream will perform better on your machine Commented May 12, 2015 at 8:58
  • I already did that and streams gave me better performance than parallel streams. This confused me. Commented May 12, 2015 at 9:06
  • So use plain streams. Parallel streams would be useful if you perform some kind of IO during processing or/and long-running calculations which you'd like to parallelize among cores. Commented May 12, 2015 at 9:09
  • @AkhilKKamal if you have local I/O you may cause too many non-sequential accesses, which bring down performance for both HDDs and SSDs. Commented May 12, 2015 at 11:18

1 Answer 1

1

Are you doing long-running operations in your streams? If yes, use parallelStream, if not, don't. Have you read the article? It refers to thread pool exhaustion during long-running operations. Is this the case for your problem/program?

As an aside, simply do Arrays.asList(entity.getChildNames()).parallelStream()...., no need to copy everything.

Sign up to request clarification or add additional context in comments.

2 Comments

My uses cases has I/O operations, so it is recommended that I should go for parallel streams.
The question you linked to is about arrays of primitives, but that does not apply to your case, as the child names are objects (you test for equality with null)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.