Reputation: 1045
I have a service for file downloading in my service I have a Asynctask that do downloding and i am using from a content provider for save status of files .my problem is that when asynctask run any other asynctask cant run in app all of them wait till download task finish.any idea?
Upvotes: 0
Views: 243
Reputation: 1902
As of honeycomb version, AsynchTasks run in a serial executor. So if you plan to run many download jobs in parallel then what you need to do is to use an ExecutorService in your Asynchtask to do jobs in parallel.
You can also set the executor type in AsynchTask to parallel instead of sequencial when you create it, but i wont recommend that.
here are some code snippets using a Loader:
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);
}
}
This is not the complete piece but should be enough to give you ideas about how to use it.
Upvotes: 1
Reputation: 1857
All AsyncTasks background work runs serially in a background worker thread, it is designed to solve short problems.
As you probably using a long running connection, you should do this on a Thread inside a separate Service. A simple approach would be receive the connection parameter by Intent and start a new thread directly on startCommand().
Upvotes: 1