Too many futures

I have a function that takes 2 functions, one that watches for a specific event and the other that actually does the job, each of which fires in the future as soon as the event returns to the thread causing the downstream message. if the worker thread ends before the event has received the caller's stream, then the observer signal stops and then the caller returns.

This works fine, but the fact is that a work function that does / can do the actual work needs to check for other events in the queue, every time I need to monitor a specific event, I launch 2 additional futures. The question is, is there a limit to the number of running max futures? Sometimes I can get 60 futures? Will the pool flow grow as needed? and since they run in the thread pool, I assume they are not too expensive to create?

+4
source share
1 answer

It all depends on the Executor . Suppose you created Executor with Executors.newFixedThreadPool (n) and sent n * 100 tasks to it, no more than n threads will work .

If you use Clojure's future function , it will send your tasks to clojure.lang.Agent.soloExecutor . SoloExecutor is created with Executors.newCachedThreadPool (threadFactory) , so it will reuse threads and run as many as possible.

+4
source

All Articles