multithreading - Parallelization (Java 8) vs Concurrency (Java 7) -


i want parse multiple files extract required data , write output xml file. have used callable interface implement this. colleague asked me use java 8 feature job easily. confused 1 of them should use now.

list.parallelstream().foreach(a -> {             system.out.println(a);         }); 

using concurrency or parallel stream helps if have independent tasks work on. example of when wouldn't locking on shared resources e.g.

// makes no sense use parallel here. list.parallelstream().foreach(a -> {         // locks system.out 1 thread @ time can work.         system.out.println(a);     }); 

however, general question, use parallelstream processing data, instead of concurrency libraries directly because;

  • a functional style of coding discourages shared mutable state. (actually how not supposed have mutable state in functional programming java not functional language)
  • it's easier write , understand processing data.
  • it's easier test whether using parallel helps or not. ti won't , can change being serial.

imho given chances using parallel coding low, best feature of parallelstream not how simple add, how simple take out.

the concurrency library better if have ad hoc work difficult model stream of data. e.g. worker pool client requests might simplier implement using executorservice.


Comments