I have a AsyncTask
called UploadManager
in my Android Application, that checks for processed items, and uploads them to the server.
For this purpose I'm using the ScheduledExecutorService.scheduleAtFixedRate
to check for items after every 1 minute, and upload them to server.
However, at times, there are multiple pools of thread created (this happens like 10% of the times), due to which at times the same request is send twice to the server, though this concurrency is handled at both the server & client level, but I still don't want this to happen on the client side.
Below is how the code looks like.
In the MainActivity (start-activity), I initiate the UploadManager as:
public class MainActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.
.
.
new UploadManager().execute(this);
}
}
UploadManager
works as follows:
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");
.
.
}
}
}
When I checked the logs, it says:
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
Which clearly indicates that there are multiple pools of thread now, due to which, uploadAcknowledgement()
method is called multiple-times.
The MainActivity is declared in AndroidManifest.xml as:
<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>
.
.