7

Let say I have an array with a thousands independent objects inside. Now I want to pass over each of them and perform the same operation, for instance, change the value of a specific field.

At first glance, there are multiple approaches in Java 8 to such task, for instance:

  1. forEach(); with lambda expression inside:
    itemsArr.forEach(item -> item.setValue("test"));
  2. forEach(); with iterator.
  3. Separate array to a number batches/blocks and deal each batch in a separate thread. For instance: define 2 threads, elements from #0 to 999 will be executed in a thread «A» and the rest in a thread «B».

The final result should be: 100% of array elements should have cared.
What is the optimal approach to such task?

Update: There is a similar question but talks about another aspect, I'm interesting not in different types of loops performance comparison (while, for, forEach) but in a performance comparison of threats vs. lambdas in a task of array traversal?

Mike
  • 14,010
  • 29
  • 101
  • 161
  • If performance is not a big deal, then can you please explain what do you mean by "preferable"? – user3707125 Jan 09 '16 at 17:14
  • Having one array containing a million objects is a bad idea in the first place. It depends on what the types of the objects are but I'm sure you can find an alternative to it. Then if you need to modify each individual value, there is no other choice but to iterate through each of them. How to iterate through them then depends on your data structure – Gaktan Jan 09 '16 at 17:15
  • @Gaktan, with *millions objects* I really overstated, in facts there are something about thousands of objects. I updated the question in order to make it more clear. – Mike Jan 09 '16 at 17:24
  • @MikeB., with thousands instead of millions your question totally loses its meaning, there is no benefit in multithreading usage on such a small samples. – user3707125 Jan 09 '16 at 17:28
  • @user3707125, I don't exclude the situation, when the collection will consist of 10K…50K as well. The main pointer is what's the best manually define couple of threads and proceed an array in multiple threads or execute the processing in a single thread with lambdas. – Mike Jan 09 '16 at 17:32

1 Answers1

16

Use a parallel stream, which the JVM will process using multiple threads:

Arrays.stream(itemsArr).parallel().forEach(item -> item.setValue("test"));

Although you seem to have a Collection, not an array, so:

itemsArr.parallelStream().forEach(item -> item.setValue("test"));
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Thanks, Bohemian. It seems like what I'm looking for. One more question, how many threads this approach generates? Is it like `ForkJoinPool`, which according to this http://stackoverflow.com/a/21172732/462347 by default has one less threads as you have processors, as returned by `Runtime.getRuntime().availableProcessors()`. – Mike Jan 09 '16 at 17:50
  • @MikeB., don't forget that FJP uses caller thread for its work as well if the job is submited from outside and caller waits till job finish. So in practice it will be equal to number of available processors. – Tagir Valeev Jan 10 '16 at 08:37
  • @TagirValeev, OK. BTW, I read number of negative feedback about `parallelStream()` in Java 8, about its side effects and pitfalls. Are you using it in production? – Mike Jan 10 '16 at 09:26
  • 1
    @MikeB., nope, I rarely have problems in production where `parallel()` would be suitable. – Tagir Valeev Jan 10 '16 at 09:53