gpt4 book ai didi

Android线程队列

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:34 24 4
gpt4 key购买 nike

我希望在单独的线程上完成一系列工作/任务,但一次只能处理一项工作。所以不能同时。

有内置的 android 吗?

谢谢,

编辑:工作 = 从数据库中获取信息。完成后,使用获取的信息更新 UI。

最佳答案

你检查过 java.util.concurrent.Executors 了吗?你可以这样做:

final static ExecutorService tpe = Executors.newSingleThreadExecutor();
...
tpe.submit(new Runnable() {
@Override
public void run() {
// your work
}
}):

它不是 android 特有的,它是 jdk5 的一部分。

来自文档:

Creates an Executor that uses a single worker thread operating off an unbounded queue. (Note however that if this single thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.) Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time. Unlike the otherwise equivalent newFixedThreadPool(1) the returned executor is guaranteed not to be reconfigurable to use additional threads.

关于Android线程队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8389585/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com