1
votes

I am learning Executor service and trying to understand how can I share list of data with the threads in the thread pool. My runnable method needs to read the data from the list and process it .

   Runnable runnable = () -> {
      System.out.println("Inside : " + Thread.currentThread().getName());
      process(list.take());
    };
  ExecutorService executor = Executors.newFixedThreadPool(threadCount);
  executorService.submit(runnable);

All the threads in the pool should process different elements from the list only once

1
Is the list used like a queue, so that while processing, new items can (and will) be added or is it just a fixed list that has to be crunched down? - Fildor
@Fildor- It will be a fixed list - par123

1 Answers

2
votes

You can loop over each element of your list and submit them to the executor:

list.forEach(e -> executor.submit(() -> process(e)));