In my app there r 6-9 AsyncTask they get fired by onlick event ,but after 5 Asynctask other asynctask doesnot run in 2.1 emulator.on DDMS Thread view I can see all 5 AsyncTask are on waiting status hence other asycntask dosent.
Any help would apprecited .Thanks in advance.
By default in most versions of Android, only 5 threads are spawned to process AsyncTasks unless the queue is full. This is fine though as hopefully you're not counting on these AsyncTasks to be processed in any sort of order, any of the others will get processed once any of the first 5 are done.
Try to lauch one after another, or do you want all of them to be executed at once, i fear that as i remember that atmost 5 task can run simultaneously, though i the latest api i think this also has changed.
THANKS
Related
I am working on an app and want to send some data to server. I was able to send the data to server when the updates that I was sending to server were periodic.
But now Its upon user interaction. I have to send 1 or more calls to server. I cant skip and omit any call towards server. Just suppose the following case:
Case:
Let say I have a button on its click I launch a asyntask that put some data on server and let suppose it takes 5 to 6 seconds. But I want to trigger multiple asyntask , if user press the button multiple time. Let suppose he presses the button 7 time in a row. so the 7 AsyncTask must run in a queue one after an other.
I have heard about schedular and all others but I am unable to understand how to work with them using asynctask as well.
Possible solution : I can disable button until and unless first Async task is not completed, But I really do not want to do it, because under the requirements and needs I have to call async task as many times as user presses the button.
please help me, any source code will be appreciated.
Note: With asynctask I have nothing to update the UI.
Update :
under my need I want to queue the asynctask with specific data and it must automatically run when the previous task is completed and if user exit the application it should continue running until and unless it completes the execution of all queued asynctask.
Let suppose he presses the button 7 time in a row. so the 7 AsyncTask must run in a queue one after an other.
Depending on what API your app target you may simply need to do nothing special. Official documentation reads:
Order of execution
When first introduced, AsyncTasks were executed
serially on a single background thread. Starting with DONUT, this was
changed to a pool of threads allowing multiple tasks to operate in
parallel. Starting with HONEYCOMB, tasks are executed on a single
thread to avoid common application errors caused by parallel execution
therefore starting from Android 3.0 execute() is all you need really, which means unless you support ancient versions of the platform you should be seeing serial execution by default.
Alternatively, you can drop AsyncTask in favor of IntentService which are executed one after another.
I don't get this, and it's really frustrating me.
All of my long-running operations happen in AsyncTasks. For example, the user interacts with the app somehow and an item is added to a list. This causes an AsyncTask to fire, the data to be written to an sqlite3 database, and then the list is refreshed.
I've recently bumped my Android support up from 2.3.3 to 4.0. I've created a new 4.0 emulator and, for some reason, all of my AsyncTasks seem to queue up and only fire once the user leaves the screen!
Why is this happening????
The code is pretty simple. I'm not doing anything interesting here. For example, here's an AsyncTask fire example:
Log log = data.mFoodLog;
log.setUserId(getUserId());
log.setDay(mBeginningOfToday.getTimeInMillis()/1000);
log.setStatus(DbDefinitions.STATE_POSTING);
(new SaveLogTask()).execute(new SaveObjectTaskParams(getApplicationContext(), log));
The debugger shows this code being executed. I have another breakpoint inside the doInBackground() method, but that breakpoint never gets hit until I leave the screen. I've also put Log.e messages around this code and ran it without the debugger to make sure the debugger isn't doing anything weird. doInBackground() never gets fired until I leave the Activity.
At first sight seems to be what #Cliffroot says, an async task not ending and queuing the ret of them (due to the behaviour changes in Android).
To execute them on diferent threads use: asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
Basically the behaviour of AsyncTask has changed since version 3.0. On 2.3.3 it used to execute AsyncTasks concurrently, but now it does it sequentially.
So your problem might be that you have one AsyncTask that does not finish, and because of that fact the others don't even start (it's hard to say without seeing more of your code).
I wrote an AsyncTask and most of the time there is no delay between its constructor been called and its doInBackground been called (0 ms delay).
But whenever contacts syncing is happening at the background, I often experience 1-3 seconds delay between my AsyncTasks's constructor and doInBackground. This delay is unacceptable in my circumstances.
I understand that AsyncTask is a background thread and this problem can be solved by using Thread and setting its priority higher. But what I want to found out is, how do I know what's causing my AsyncTask's doInBackground from being called?
I used adb shell top -m 10 and the process usage seems quite normal when this issue happened.
Any help is appreciated.
thanks
I also face this issue for long period, but now it is solved. Use the code below
new AsyncTaskName().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
instead the code
new AsyncTaskName().execute();
it would solve the problem of running doInbackground late.
We generally don't bother about task scheduling by the jvm. And infact we need not to bother also.
If something needs to be done as quick as possible in your application do it in the constructor itself or use onPre of Asynctask (Remember it execute on UI thread).
But i agree there is something fishy in the doInBackgroud calling in Android AsyncTask
i itself had witnessed the doInbackground didn't get called after onPre. You can google this also. many people had faced it.
I moved to use Traditional Thread.
I wrote my own Asynctask using Traditional thread at core and to mimic onPre and onPost i used Handler. You can go for that option also.
It's important to distinguish between creating and executing the task. (An ASyncTask has a separate execute() method, as well as a constructor, as you have noticed.)
Creating a thread is potentially quite expensive, so you might find that creating the task in advance, and then only executing it at the correct time, gives better results.
If the background operation is likely to be repeated often, you may also find that an IntentService, which handles requests one-at-a-time in a background thread, is more suitable.
According to this question there is a limit on the number of running asynctasks. I'm using asynctasks in several activities and in each activity I cancel running asynctask (if any) before execute a new one. but still I have blocking problem and I doubt the asynctask are completely canceled. My question is that "is there any way to see all running and blocking asynctasks?"
thanks in advance!
AsyncTask is execute In the ThreadPool. Before HoneyComb the default size is 5. after the default size is 1.
When you call the AsyncTask.cancel() . the Thread is not exactly cancelled.
My program involves interaction with SQLite in a fairly regular basis, and in the beginning of the app, I call a query
mDatabase.rawQuery("SELECT key,
indice as _id
FROM Dictionary",null);
Strangely the app stalls on executing this line. This does not happen if I am debugging the application, but when I run the app, the control goes from this line and never returns. I have checked this by putting logcat before and after this line.
I have not been able to comprehend this behavior. Can someone help?
P.S. The table Dictionary has over 2000-3000 records.
EDIT:
I have tried calling this from both UI & separate threads. Either ways, the execution stops at this call (for that thread). So when I call it from another thread, though there is no ANR, the call still fails and holds the thread indefinitely.
EDIT2:
This issue does not happen every time I run the application but 5 out of 10 times. And apparently happens more on weaker phones.
Take care of below points.
Make call to query in separate thread other than UI thread.
Cursor at max can hold upto 1MB of data. So query for minimum amount of data.
You should take this off the UI thread. Looks like a heavy call. Anything which takes longer than 5 seconds and stalls the UI thread will trigger an ANR.
Yes piyushnp and abhinav are right. AsyncTask or Thread are better option for getting the details. Show progressbar when doing background processing. And when you get query results display it in activity or do whatver processing you want on query results.
This example simulates your problem. AsyncTask basic Example : AsyncTask