如何在后台执行任务而不影响UI线程?

时间:2022-11-10 20:56:16

I'm making some special Android Launcher, many clicks in the UI are supposed to do some heavy operations before the result is done. I don't want the UI to hang while that's executing. I don't want to create a Thread on every job because that's resource intensive.

我正在制作一些特殊的Android启动器,在结果完成之前,UI中的许多点击都应该做一些繁重的操作。我不希望UI在执行时挂起。我不想在每个作业上创建一个Thread,因为这是资源密集型的。

Note that I want the Threads to be executed in the order they happened, one after another.

请注意,我希望线程按照它们发生的顺序一个接一个地执行。

2 个解决方案

#1


One good strategy that works in cases like this is to create one thread whose only purpose is to: "be the boss." Uh huh, "there s/he is, just sitting' on his/her (!), not actually doing anything ...", but, this thread knows, at all times, what the current overall workflow looks like. Every other thread "asks the boss what to do next," and every other thread "reports to the boss the outcome of what has just been done."

在这种情况下工作的一个好策略是创建一个线程,其唯一目的是:“成为老板。”嗯,“他/她是,只是坐在他/她(!)上,实际上并没有做任何事......”但是,这个线程在任何时候都知道当前的整体工作流程是什么样的。每一个其他主题“都要求老板下一步做什么”,而其他每一个主题都“向老板报告刚刚完成的工作的结果”。

The reason for this design is: "the management of a workflow" is usually not "an asynchronous activity." In fact, it is prohibitively "full of race-conditions," as we all have seen in any human-situation where "nobody's in charge."

这种设计的原因是:“工作流程的管理”通常不是“异步活动”。事实上,正如我们所有人都看到的那样“没有人负责”,这种情况令人望而却步“充满了竞争条件”。

Therefore: "put one thread 'in charge.'" One thread maintains the "to-do list." Any subordinate thread can grab a "to-do" from the queue that this one thread is responsible for maintaining, and upon completion of that activity it must submit a status-report to "that one boss-thread." Thus, "that one boss-thread," responding serially to various streams of requests, can effectively direct the activities of everyone else.

因此:“放一个线程”负责。'“一个线程维护”待办事项列表“。任何从属线程都可以从这个线程负责维护的队列中获取“待办事项”,并且在完成该活动后,它必须向“那个boss-thread”提交状态报告。因此,“一个老板 - 线程”,连续响应各种请求流,可以有效地指导其他人的活动。

"That one thread" is the only authority who knows that "this app has been added to the sidebar." (It was notified of this from a message on a serialized queue that only it reads.) Based on this knowledge, it now creates one-or-more "to-dos," as the workflow-definition dictates. Soon enough, some subordinate will pick up that to-do and "run with it," not knowing why it was posted. (It doesn't have to "know why.") It will, in due time, complete the work, post a completion-report to the "boss," and await further instructions.)

“那个线程”是唯一知道“此应用已被添加到侧边栏中”的权威机构。 (它是通过序列化队列上的消息通知的,只有它才读取。)基于这些知识,它现在创建一个或多个“待办事项”,正如工作流定义所规定的那样。很快,一些下属将接受那个待办事项并“与它一起运行”,不知道为什么发布。 (它不必“知道原因。”)它将在适当的时候完成工作,将完成报告发布给“老板”,并等待进一步的指示。)

It's a strategy that works great for human activities. So, it works equally well for computers, too.

这是一项适合人类活动的策略。因此,它对计算机同样有效。

#2


The best way to communicate to your UI is to use an EventBus either from Greebrobot or from Square.

与UI进行通信的最佳方式是使用Greebrobot或Square中的EventBus。

RxJava is very good in bouncing between different threads, learning curve is quite high though.

RxJava非常适合在不同的线程之间弹跳,但学习曲线相当高。

Android-Priority-Job-Queue is also worth checking.

Android-Priority-Job-Queue也值得一试。

#1


One good strategy that works in cases like this is to create one thread whose only purpose is to: "be the boss." Uh huh, "there s/he is, just sitting' on his/her (!), not actually doing anything ...", but, this thread knows, at all times, what the current overall workflow looks like. Every other thread "asks the boss what to do next," and every other thread "reports to the boss the outcome of what has just been done."

在这种情况下工作的一个好策略是创建一个线程,其唯一目的是:“成为老板。”嗯,“他/她是,只是坐在他/她(!)上,实际上并没有做任何事......”但是,这个线程在任何时候都知道当前的整体工作流程是什么样的。每一个其他主题“都要求老板下一步做什么”,而其他每一个主题都“向老板报告刚刚完成的工作的结果”。

The reason for this design is: "the management of a workflow" is usually not "an asynchronous activity." In fact, it is prohibitively "full of race-conditions," as we all have seen in any human-situation where "nobody's in charge."

这种设计的原因是:“工作流程的管理”通常不是“异步活动”。事实上,正如我们所有人都看到的那样“没有人负责”,这种情况令人望而却步“充满了竞争条件”。

Therefore: "put one thread 'in charge.'" One thread maintains the "to-do list." Any subordinate thread can grab a "to-do" from the queue that this one thread is responsible for maintaining, and upon completion of that activity it must submit a status-report to "that one boss-thread." Thus, "that one boss-thread," responding serially to various streams of requests, can effectively direct the activities of everyone else.

因此:“放一个线程”负责。'“一个线程维护”待办事项列表“。任何从属线程都可以从这个线程负责维护的队列中获取“待办事项”,并且在完成该活动后,它必须向“那个boss-thread”提交状态报告。因此,“一个老板 - 线程”,连续响应各种请求流,可以有效地指导其他人的活动。

"That one thread" is the only authority who knows that "this app has been added to the sidebar." (It was notified of this from a message on a serialized queue that only it reads.) Based on this knowledge, it now creates one-or-more "to-dos," as the workflow-definition dictates. Soon enough, some subordinate will pick up that to-do and "run with it," not knowing why it was posted. (It doesn't have to "know why.") It will, in due time, complete the work, post a completion-report to the "boss," and await further instructions.)

“那个线程”是唯一知道“此应用已被添加到侧边栏中”的权威机构。 (它是通过序列化队列上的消息通知的,只有它才读取。)基于这些知识,它现在创建一个或多个“待办事项”,正如工作流定义所规定的那样。很快,一些下属将接受那个待办事项并“与它一起运行”,不知道为什么发布。 (它不必“知道原因。”)它将在适当的时候完成工作,将完成报告发布给“老板”,并等待进一步的指示。)

It's a strategy that works great for human activities. So, it works equally well for computers, too.

这是一项适合人类活动的策略。因此,它对计算机同样有效。

#2


The best way to communicate to your UI is to use an EventBus either from Greebrobot or from Square.

与UI进行通信的最佳方式是使用Greebrobot或Square中的EventBus。

RxJava is very good in bouncing between different threads, learning curve is quite high though.

RxJava非常适合在不同的线程之间弹跳,但学习曲线相当高。

Android-Priority-Job-Queue is also worth checking.

Android-Priority-Job-Queue也值得一试。