我在我的 Android 应用程序中有一个AsyncTask
调用UploadManager
,它检查已处理的项目,并将它们上传到服务器。
为此,我使用ScheduledExecutorService.scheduleAtFixedRate
1 分钟后检查项目,并将它们上传到服务器。
但是,有时会创建多个线程池(这种情况大约有 10% 的情况发生),因此有时会向服务器发送两次相同的请求,尽管这种并发性是在服务器和客户端级别处理的,但我仍然不希望这种情况发生在客户端。
下面是代码的样子。
在 MainActivity (start-activity) 中,我将 UploadManager 启动为:
public class MainActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
new UploadManager().execute(this);
}
}
UploadManager
工作方式如下:
public class UploadManager extends AsyncTask<Context, Integer, Integer> {
private ScheduledExecutorService scheduledExecutorService;
private static final int NUM_OF_THREADS = 5;
private static final int DELAY_IN_SECONDS = 60;
private Context context;
private final Logger logger = new Logger(getClass().getSimpleName());
protected Integer doInBackground(Context... context) {
this.context = context[0];
scheduledExecutorService = Executors.newScheduledThreadPool(NUM_OF_THREADS);
scheduledExecutorService.scheduleAtFixedRate(postInformation, 5, DELAY_IN_SECONDS, TimeUnit.SECONDS);
return 0;
}
private Runnable postInformation = new Runnable() {
@Override
public void run() {
if (NetworkManager.isInternetAvailable(context)) {
uploadAcknowledgement();
}
}
};
private void uploadAcknowledgement() {
List<Acknowledgement> ackList = null;
try {
logger.info("RUNNING TASK TO POST ACKNOWLEDGEMENT");
.
.
}
}
}
当我检查日志时,它说:
35119 [pool-2-thread-1] INFO Upload Manager - [1363841355530] : 21/03/2013 10:19:15 : RUNNING TASK TO POST ACKNOWLEDGEMENT
35122 [pool-3-thread-1] INFO Upload Manager - [1363841355532] : 21/03/2013 10:19:15 : RUNNING TASK TO POST ACKNOWLEDGEMENT
这清楚地表明现在有多个线程池,因此,uploadAcknowledgement()
方法被多次调用。
MainActivity 在 AndroidManifest.xml 中声明为:
<application android:label="@string/app_name" android:icon="@drawable/icon">
<activity android:name="MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</activity>
.
.