0

Below code creates large number of runnable object in loop, even only 5 treads are there to process the tasks. Is there any way so that at any moment only 5 runnable object will be present in memory not the total number of tasks( 100000).

ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10000; i++) {
    Runnable worker = new WorkerThread("" + i);
    System.out.println("ExecutorExample.main()");
    executor.execute(worker);
}
Sandeep Bhardwaj
  • 1,310
  • 1
  • 18
  • 24
  • 2
    create 5, submit, wait for completion then create 5 again etc until you have the 10000 ?? –  Jul 05 '17 at 11:03
  • Create an `ExecutorServce` to manage the other five services and assign new jobs, that way your main thread won't hang as much. – Paul Jul 05 '17 at 11:05

1 Answers1

3

The requirement that at any moment only 5 runnable object will be present in memory is too restrictive. When a task finishes, there would be no next task to run, and some time would be spend to create another task. Better to have 5 runnung tasks and 5 waiting in a line. In order to restrict the number of the tasks in the queue, use a BlockingQueue of fixed size for runnables, e.g.

ThreadPoolExecutor executor = new ThreadPoolExecutor(
     5, 5,
     10L, TimeUnit.MILLISECONDS,
     new LinkedBlockingQueue<Runnable>(5)
)

UPDT To avoid RejectedExecutionException, add RejectedExecutionHandler, as proposed in https://stackoverflow.com/a/3518588/1206301:

RejectedExecutionHandler block = new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(r);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    executor.setRejectedExecutionHandler(block);
Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
  • Could you please tell me how to use this in above scenario. – Sandeep Bhardwaj Jul 06 '17 at 04:37
  • @SandeepBhardwaj just replace the first line in your program with what I proposed and leave the rest intact. – Alexei Kaigorodov Jul 06 '17 at 08:32
  • Only 10 tasks are run due to `Exception in thread "main" java.util.concurrent.RejectedExecutionException...` – starikoff Jul 08 '17 at 08:47
  • It's probably worth stressing the quotes from the post you are referring to: "Now. This is a very bad idea for the following reasons" (3 given), but then "However, sometimes a blocking strategy, with all its inherent risks, is really what you want". Or maybe you don't agree with the reasons to not use it given there? – starikoff Jul 08 '17 at 10:24
  • @starikoff I actually don't think this idea is that bad. For the given use case, it works well. – Alexei Kaigorodov Jul 08 '17 at 10:31
  • Sure. I would include it as a warning to whoever comes to this question and answer later with a different use case and doesn't follow the link, but it's your call definitely (: – starikoff Jul 08 '17 at 10:37