如何在android服务和应用程序中进行多任务处理
我的服务中有一个文件下载服务我有一个Asynctask进行下载,我使用内容提供商提供的文件保存状态。我的问题是,当Asynctask运行任何其他Asynctask时,所有其他Asynctask都不能在应用程序中运行,所有Asynctask都要等到下载任务完成。有什么想法吗?所有AsyncTasks后台工作都在应用程序中连续运行后台工作线程,用于解决短时间问题如何在android服务和应用程序中进行多任务处理,android,service,multitasking,Android,Service,Multitasking,我的服务中有一个文件下载服务我有一个Asynctask进行下载,我使用内容提供商提供的文件保存状态。我的问题是,当Asynctask运行任何其他Asynctask时,所有其他Asynctask都不能在应用程序中运行,所有Asynctask都要等到下载任务完成。有什么想法吗?所有AsyncTasks后台工作都在应用程序中连续运行后台工作线程,用于解决短时间问题 由于您可能使用长时间运行的连接,因此应该在单独服务内的线程上执行此操作。一种简单的方法是通过Intent接收连接参数,并直接在startC
由于您可能使用长时间运行的连接,因此应该在单独服务内的线程上执行此操作。一种简单的方法是通过Intent接收连接参数,并直接在startCommand()上启动一个新线程。从蜂窝版本开始,异步任务在串行执行器中运行。因此,如果您计划并行运行许多下载作业,那么您需要做的是在Asynchtask中使用ExecutorService来并行执行作业 您还可以在创建AsynchTask时将其执行器类型设置为并行而不是顺序,但我不建议这样做 下面是一些使用加载程序的代码片段:
private ExecutorService executorService;
private static final int MAX_RUNNABLE_THREADS = 800;
private static final int KEEP_ALIVE_TIME = 6;
//in constructor and probably in onStartLoading...
if(this.isNetworkfast){
this.executorService = new ThreadPoolExecutor(0, MAX_RUNNABLE_THREADS
, KEEP_ALIVE_TIME
, TimeUnit.SECONDS
, new SynchronousQueue<Runnable>()
);
}else {
this.executorService = Executors.newFixedThreadPool(3);
}
//in onReset
this.executorService.shutdownNow();
try {
if (executorService.awaitTermination(20, TimeUnit.SECONDS)) {
Log.i(TAG, "executorService shutdown completed.");
}else{
Log.e(TAG, "executorService shutdown failed to finish completely.");
if(this.isErrorReportingEnabled){
this.errorMap.put("error_" + (++errorCount), "executorService shutdown failed to finish completely.");
}
}
} catch (InterruptedException e) {
Log.e(TAG, "DownloadNseQuotesAsynchTaskLoader executorService shutdown interrupted.");
if(this.isErrorReportingEnabled){
this.errorReporter.reportCustomMessagesWithException("exception_" + (++errorCount)
, "DownloadNseQuotesAsynchTaskLoader executorService shutdown interrupted.", e);
}
}
//in loadInBackground....
//do your processing to determine the number of workers/runnables needed and based on that
final CountDownLatch latch = new CountDownLatch(number of workers);
MyWorker worker = new MyWorker(latch, data set....);
this.executorService.execute(worker);
//and later in the same method we wait on the latch for all workers to finish
try {
latch.await(); //blocks the current thread until the latch count is zero
//all Workers have finished, now read in the processed data if you want
for (MyWorker worker : workers) {
SomeDataVO dataVO = worker.getData();
.......
}
} catch (InterruptedException ie) {
Log.e(TAG, "Interrupted exceltion while await on CountDownLatch running", ie);
if(this.isErrorReportingEnabled){
this.errorReporter.reportCustomMessagesWithException("exception_" + (++errorCount)
, "Interrupted exception while await on CountDownLatch running", ie);
}
}
私有执行器服务;
私有静态final int MAX_RUNNABLE_THREADS=800;
私有静态最终int保持活动时间=6;
//在构造函数中,可能在onStartLoading中。。。
if(this.isNetworkfast){
this.executorService=新线程池执行器(0,最大可运行线程数)
,让你活下去
,时间单位为秒
,新的SynchronousQueue()
);
}否则{
this.executorService=Executors.newFixedThreadPool(3);
}
//自动复位
此.executorService.shutdownNow();
试一试{
if(执行器服务等待终止(20,时间单位秒)){
Log.i(标签“executorService shutdown completed.”);
}否则{
Log.e(标签“executorService shutdown未能完全完成”);
如果(此.isErrorReportingEnabled){
this.errorMap.put(“error”+(++errorCount),“executorService shutdown未能完全完成。”);
}
}
}捕捉(中断异常e){
Log.e(标记“DownloadNseQuotesAsynchTaskLoader执行器服务关闭中断”);
如果(此.isErrorReportingEnabled){
this.errorReporter.ReportCustomMessagesWitheException(“exception”+(++errorCount)
,“下载NSeQuotesAsynchtaskloader执行器服务关闭中断”,e);
}
}
//在loadInBackground。。。。
//进行处理以确定所需的工人/可运行人员的数量,并以此为基础
最终倒计时闩锁=新倒计时闩锁(工人数量);
MyWorker=新的MyWorker(闩锁、数据集…);
此.executorService.execute(工作者);
//然后用同样的方法,我们在门闩上等待所有工人完成
试一试{
latch.WAIT();//阻塞当前线程,直到闩锁计数为零
//所有工作人员都已完成,如果需要,现在读入已处理的数据
for(我的工人:工人){
SomeDataVO dataVO=worker.getData();
.......
}
}捕获(中断异常ie){
Log.e(标签“在倒数锁运行时等待时中断卓越”,即);
如果(此.isErrorReportingEnabled){
this.errorReporter.ReportCustomMessagesWitheException(“exception”+(++errorCount)
,“等待倒计时锁运行时中断异常”,即);
}
}
这不是完整的部分,但应该足以让您了解如何使用它。使用其他执行器(请参阅ExecuteOnExecutor)