Java – what happens if the executorservice queue is full
I have a large file up to TB. My task is to process it line by line Each line should take 5 seconds to complete To improve performance, I allocate processes to such a fixed thread pool
ExecutorService executor = Executors.newFixedThreadPool(5); while ((line = br.readLine()) != null) { Runnable worker = new WorkerThread(line); executor.execute(worker); }
My question is what happens if I overwhelm the execution queue by performing so many tasks Will it abandon stackoverflow?
Solution
This may be a topic, but one option for this problem is to use a fixed length blocking queue and use ThreadPoolExecutor CallerRunPolicy(). Thus, if the consumer is not fast enough (so the queue is filling up), the caller thread (producer) will be used to run the task itself We can initialize an executor as follows:
executorService = new ThreadPoolExecutor(DEFAULT_THREAD_COUNT,DEFAULT_THREAD_COUNT,2,TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(DEFAULT_QUEUE_LENGTH),new ThreadPoolExecutor.CallerRunsPolicy());
From the API: "the handler of the rejected task, which directly runs the rejected task in the calling thread of the execute method, unless the executor has been closed, in which case the task is discarded."