I have a function that takes 2 functions, one watches for a certain event and another one which actually does the work, each runs in a future, as soon as the event future returns failure caller thread signals worker future to stop. if the worker thread finishes before the event is received caller thread then signals watcher to stop and then caller returns.
This works fine but the thing is the worker function that does the actual work may/do needs to check for other events down the line, each time I need to watch for a certain event I fire 2 extra futures. The question is, is there a limit on the number of max futures running? I may end up with 60 futures at times? Will the thread pool grow as needed? and since they run on a thread pool I am assuming they are not too costly create?
It all depends on the Executor. Suppose you created a Executor
with Executors.newFixedThreadPool(n) and submitted n * 100 tasks to it, there will be no more than n threads running.
If you are using Clojure's future function it will submit your tasks to clojure.lang.Agent.soloExecutor. The soloExecutor is created with Executors.newCachedThreadPool(threadFactory), so it will re-use threads and run as many as possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With