2

Is there practically any limit on how many number of Runnable's that can be added to the queue in a fixed/cached Thread Pool of say 10 worker Threads..??

In my application, I am trying to add 100000 Runnable's.

for (int i=0; i<100000; i++) {
    executor.execute(new Task(i));
} 

Will it actually process all the Runnable's added to the Queue..? Or will it throw an Exception?

msrd0
  • 7,816
  • 9
  • 47
  • 82
surendhar_s
  • 824
  • 2
  • 12
  • 20

2 Answers2

4

Maximum number of Runnable's that can be added to the Queue in fixed/cached Thread Pool is decided by the memory allocated to the JVM.

Once all of the memory is consumed, JVM will throw

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread

error message.

msrd0
  • 7,816
  • 9
  • 47
  • 82
crashstorm
  • 130
  • 5
1

Assuming that you are using the java.util.concurrent.Executors.newFixedThreadPool() method, the limit is Integer.MAX_INT, at least in the OpenJDK implementation.

The OpenJDK 8 implementation of this method creates a new LinkedBlockingQueue, which defaults to a max size of Integer.MAX_INT.

Thus, the maximum number of Runnables in the default fixed thread pool is Integer.MAX_INT. Note however, that if your heap is not large enough to store that many Runnables, then you will still be limited by the size of your heap.

therealrootuser
  • 10,215
  • 7
  • 31
  • 46