轻量级、可折叠的 Executor 实现?

发布于 2024-10-19 05:41:26 字数 206 浏览 6 评论 0原文

我正在构建一个 Android 移动应用程序,我需要为每个列表适配器汇集 HTTP 请求。我基本上想要一个“崩溃”的 ExecutorService 实现,即:它将使用最多 n 个线程,但当线程完成时,它们将立即过期,从而使其真正轻量级。如果需求很高,它只会将任务转储到队列中,等待线程变得可用。有没有办法在不自己编写 ExecutorService 的情况下做到这一点,或者我应该亲自动手去做呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巴黎夜雨 2024-10-26 05:41:26

使用 ThreadPoolExecutor 及其 keepAliveTime 设置为零?

例如,

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

来自 setKeepAliveTime() 的文档:

时间值为零将导致多余的线程在执行任务后立即终止。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文