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?

  • make performance test to see if parallelStream will perform better on your machine – Alex Salauyou May 12 '15 at 08:58
  • I already did that and streams gave me better performance than parallel streams. This confused me. – Akhil K Kamal May 12 '15 at 09: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. – Alex Salauyou May 12 '15 at 09: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. – Tassos Bassoukos May 12 '15 at 11:18

1 Answers1

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • My uses cases has I/O operations, so it is recommended that I should go for parallel streams. – Akhil K Kamal May 12 '15 at 09:29
  • 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) – Tassos Bassoukos May 12 '15 at 11:19