简体   繁体   English

如何管理Sync Task中的线程数

[英]How to manage number of threads inside Sync Task

What i am looking for is to find a way to manage a number of threads inside the Asyntask. 我正在寻找的是找到一种方法来管理Asyntask内的许多线程。

I need to sync my local database with server updates. 我需要将本地数据库与服务器更新同步。 This might be large data that can be retrieved using paging. 这可能是可以使用分页检索的大数据。

Can anybody tell me what is preferred way to manage generating threads in loop inside the asyntask so that it could display Progress Dialog until all of the thread completed. 任何人都可以告诉我在asctiask内循环管理生成线程的首选方法是什么,以便它可以显示进度对话直到所有线程都完成。

I am little worried about the local database object & its synchronization during execution of different threads in order to download data from server & save/update into local database. 我担心本地数据库对象及其在执行不同线程期间的同步,以便从服务器下载数据并保存/更新到本地数据库。

Note: I cant download all data from server in a single server call. 注意:我无法在单个服务器调用中从服务器下载所有数据。

Thanks in advance. 提前致谢。

If it's long running operation you should use IntentService instead of AsyncTask. 如果它是长时间运行的操作,您应该使用IntentService而不是AsyncTask。 IntentService is UI independent so you can run it in background. IntentService独立于UI,因此您可以在后台运行它。 To update UI you can send broadcast intent. 要更新UI,您可以发送广播意图。 I use IntentService for DB sync. 我使用IntentService进行数据库同步。 I get data from server, while parsing I create batch updates and then update local DB with one batch update. 我从服务器获取数据,在解析时创建批量更新,然后通过一次批量更新更新本地数据库。

Try this ,    

You can set a static variable that is initialized to zero, and it is incremented 
by each thread .check this variable in asynctask,if it is equal to the no: of threads 
in your class finish it.Simple But works

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM