jiekang / thermostat-web-api

Git mirror of icedtea.classpath.org/people/jkang/thermostat-web-api/
2 stars 2 forks source link

Consider using a threadpool instead of creating new threads #3

Open thescouser89 opened 7 years ago

thescouser89 commented 7 years ago

In the JAX-RS methods, we are using AsyncResponse to improve the performance of REST requests.

The pattern used however causes new threads to be created for every request. This isn't ideal because in heavy load, the thread count could become so big that it could bring the whole system to a halt (through resource exhaustion, for e.g too much RAM or thrashing happening).

I would recommend that we instead switch to a threadpool design. If Java 8 is the target of this project, there are excellent resources available to setup threadpools by using executors (also see CompletableFutures for chainable futures).

The threadpool design will allow us to keep the thread count bounded to a certain bound in heavy load.

If Java 8 is not an option, maybe we could also have a look at RxJava.

jiekang commented 7 years ago

Excellent point. I think Java 7 has ThreadPool classes as well, [1], for example. I'll take a look at this.

[1] https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ThreadPoolExecutor.html