I got these kind of exception similar to AsyncTask, RejectedExecutionException and Task Limit, and “grab code and add to my package”.My question is: is this the right anwser, anyone have other official solutions?
java.util.concurrent.RejectedExecutionException: Task android.os.AsyncTask$3@416cd2e0 rejected from java.util.concurrent.ThreadPoolExecutor@4101be78[Running, pool size = 128, active threads = 128, queued tasks = 10, completed tasks = 53]
updateTimer.schedule(updateTask, 500, 3000);
…
updateTask = new TimerTask() {
public void run()
{
updateHandler.sendMessage(msg);
}
};
…
updateHandler = new Handler() {
public void handleMessage(Message msg)
{
updateAsync();
}
};
…
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... params)
{
retrieveStatus();
return null;
}
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
updateStatusUi();// !!!?
}
}.execute();
updateTask = new TimerTask() {
public void run()
{
retrieveStatus();
getActivity().runOnUiThread(new Runnable() {
public void run()
{
updateStatusUi();
}
});
}
};
IN ICS: After about 10 times, it never update any more. Can you please give me a Demo, so I can pass some rigor test like: MainActivity: open(), close(), …
P.S. myAsyncTask.executeOnExecutor(MyAsyncTask.SERIAL_EXECUTOR, [params] ); will work?
First, you are forking a new thread every 500ms. This is really bad, on any OS, in any programming language.
Second, you are forking yet another thread (your
TimerTask), that is posting a message back to the main application thread, that is turning around and forking theAsyncTask. This is pointless.If you are going to use a
TimerTask, simply callretrieveStatus()from yourrun()method, then userunOnUiThread()to executeupdateStatusUi()on the main application thread. Now you are only using one background thread (theTimerTask) and introducing much less overhead to accomplish the same ends.Or, consider implementing your own
ScheduledExecutorServiceinstead of usingTimerTask.