安卓的AsyncTask - 避免运行多个实例 [英] Android AsyncTask - avoid multiple instances running

查看:136
本文介绍了安卓的AsyncTask - 避免运行多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的AsyncTask是处理一些背景HTTP的东西。 AsyncTask的运行时间表(报警/服务)和某个用户手动执行它。

I have AsyncTask that processes some background HTTP stuff. AsyncTask runs on schedule (Alarms/service) and sometime user executes it manually.

我处理来自SQLite的记录,我注意到了服务器,告诉我,有时计划任务运行,并在同一时间的用户运行手动导致相同的记录被读取并从数据库处理两次双职位。我删除记录他们处理之后,但仍然得到这一点。

I process records from SQLite and I noticed double-posts on server which tells me that sometime scheduled task runs and at the same time user runs it manually causing same record to be read and processed from DB twice. I remove records after they processed but still get this.

我应该如何处理呢?也许组织一些queing的?

How should I handle it ? Maybe organize some kind of queing?

推荐答案

您可以在一个执行程序使用<一个执行你的AsyncTask的href="http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor%28java.util.concurrent.Executor,%20Params...%29"><$c$c>executeOnExecutor()

You can execute your AsyncTask's on an Executor using executeOnExecutor()

要确保该线程以串行方式运行,请使用: SERIAL_EXECUTOR

To make sure that the threads are running in a serial fashion please use: SERIAL_EXECUTOR.

其它:如何使用一个Executor

如果几个活动正在访问你的数据库为什么不创造一种网关数据库帮手,并使用同步块,以确保只有一个线程可以访问它以瞬间

If several activities are accessing your DB why don't create a sort of gateway database helper and use the synchronized block to ensure only one thread has access to it at an instant

这篇关于安卓的AsyncTask - 避免运行多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆