1

I have a list which contains X number of records. I want to process these records using multiple threads. Currently I have taking out the records and sending it for execution. When I printed the threadId and the record name, I can see that multiple threads are trying to execute the logic for same record which should not be the case. How can I limit this ?

  • It is impossible to tell without looking into your code. Can you post minimal working example https://stackoverflow.com/help/mcve – talex Sep 06 '18 at 09:02
  • Possible duplicate of [Java - splitting work to multiple threads](https://stackoverflow.com/questions/12845881/java-splitting-work-to-multiple-threads) – Pavel Gordon Sep 06 '18 at 09:02

1 Answers1

0

You must be passing the same record multiple times to the pool. There is no easy way to accidentally do what you suggest so it is hard to image anything else.

BTW If you don't know if the record are unique, you can use Steam.distinct().

List<Result> results = list.stream()
                           .distinct()
                           .parallel()
                           .peek(r -> System.out.println(
                                               Thread.currentThread() + " processes " + r);
                           .map(r -> processRecord(r))
                           .collect(Collectors.toList());

Streams are often an easier way to process records in a collection across multiple threads.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130