Lightweight, collapsible Executor implementation?

791 Views Asked by At

I'm building a mobile app for Android and I need to pool HTTP requests for each of my List adapters. I basically want an ExecutorService implementation that "collapses," ie: it will use up to n threads, but as threads complete, they will immediately expire, making it really lightweight. If there's high demand, it'll just dump tasks into a queue which will wait for threads to become available. Is there a way to do this without writing an ExecutorService myself or should I just get my hands dirty and do it?

1

There are 1 best solutions below

6
On BEST ANSWER

Would it work to use a ThreadPoolExecutor with its keepAliveTime set to zero?

e.g.

int core = 5;
int max = 20;
new ThreadPoolExecutor(core, max, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>())

From the docs for setKeepAliveTime():

A time value of zero will cause excess threads to terminate immediately after executing tasks.