Efficiently processing many small elements of a collection concurrently in Java

43 views Asked by At

What is the most efficient way in Java to perform a small CPU intensive task for a large number of elements in a data structure?

Example Situation:

  • I have a large (e.g. 100,000+) number of elements in a Java data structure (a 2-level List of Lists with variable sizes. The sublists might have a size of 1-10,000+, they are quote unpredictable).
  • I need to do a small but non-trivial amount of CPU-intensive processing on each element of each child list (I have timed about 0.1-0.2ms each, roughly equal for each task)
  • The goal is to complete processing as fast as possible with minimal overhead / wasted CPU resources

Additional notes / assumptions:

  • Target machine is likely to have 8+ logical processors available (often more, may sometimes be less)
  • The tasks do no blocking IO or logging, they are purely CPU computation
  • Target Java VM is 11+, but it is quite likely to be 21+ so virtual threads may be available and would be good to make use of these if helpful.
  • Order of processing does not matter
  • The operations are individually thread safe and idempotent, so it doesn't matter if an element gets processed twice (though obviously wasteful)

What is the most efficient way to do this in Java?

NOTE: I know it is possible to create an ExecutorService and submit a lot of small Runnable tasks but this might create a lot of memory allocations / garbage / thread switching overhead given that the tasks are individually quite small.

Therefore I am interested in answers that find the most efficient overall approach (and reasoning for why this is optimal in this situation).

0

There are 0 answers