当IntentService完成时,由IntentService生成的线程会发生什么?(What happens with threads spawned by an IntentService wh

编程入门 行业动态 更新时间:2024-10-24 18:17:04
当IntentService完成时,由IntentService生成的线程会发生什么?(What happens with threads spawned by an IntentService when IntentService finishes?)

我有一个IntentService接收数据并将其写入本地数据库。 然后处理DB数据。 目前我在onHandleIntent()方法中完成所有操作。

问题:数据不会立即存储在DB中。 但它应该是。 而是在第一次完成后处理下一个数据块。

数据块将同步处理。

我的解决方案:

1)我可以使用Service而不是IntentService 。 但是,我想避免这种情况,因为我想避免处理异步内容。

2)我可以使用两个IntentService :第一个接受数据并存储它。 第二个读取并处理它。

3)我可以生成一个WorkerThread (或使用ExecutorService )来处理数据。 这是我想要的方式。

现在,我的问题:当IntentService离开onHandleIntent()方法时, IntentService产生的线程会发生什么? 有什么问题可以预料吗? 应该使用选项2)还是1)?

I have an IntentService which receives data and writes it into a local DB. Then the DB data is to be processed. Currently I am doing it all in the onHandleIntent() method.

Problem: Data is not stored immediately in DB. But it should be. Instead next data block is processed after first has finished.

Data blocks are to be processed synchronously.

My solution ideas:

1) I could use a Service instead of IntentService. However, I want to avoid that because I want to avoid handling the async stuff.

2) I could use two IntentServices: The first accepts the data and stores it. The second reads it and processes it.

3) I could spawn a WorkerThread (or use an ExecutorService) to process the data. This is the way I would like to go.

Now, my question: What happens to the thread spawned by the IntentService when the IntentService leaves its onHandleIntent() method? Are there any problems to be expected? Should rather use option 2) or 1)?

最满意答案

从IntentService派生的Thread绑定到IntentService对象(pid)的生命周期。

如果处理应该是同步的,那么你应该选择2。

Threads spawned from IntentService are bonded to the lifetime of the IntentService object (pid).

If the processing should be synchronous exclusively, you should go with option 2.

更多推荐

本文发布于:2023-07-27 17:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   发生   IntentService   finishes   spawned

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!