1

What is the best approach to parallel process a Collection of Java Objects? I would like to have a threadpool of a 100 threads each work on a separate Collection object and perform some action on it. Any ideas? Java 8 is the targeted version.

2
  • I'm not sure that you should have a thread pool here. Let the JVM and parallel stream figure it out, based on the number of cores it has available. It'll do a better job of optimizing that you will. Commented Mar 10, 2016 at 1:19
  • When Java allows you to configure the thread pool used by parallelStream, you'll be able to do both. For now, you have to live with what you're given. It's good enough much of the time, but if you're processing many slow operations, you may want to do it yourself using @c12's original (Java <8) idea. Commented Mar 10, 2016 at 1:24

1 Answer 1

4

Use a parallelStream.

yourCollection
  .parallelStream()
  .forEach(e -> doStuff(e));

You may also want to collect() the results afterwards.

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

Comments

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.