Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many Futures

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?

like image 574
Hamza Yerlikaya Avatar asked Sep 05 '11 15:09

Hamza Yerlikaya


1 Answers

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.

like image 139
iruediger Avatar answered Nov 07 '22 23:11

iruediger