Executing a runnable in Asynctask
Solution 1
It's the same as execute()
but it will run your Runnable
in the background instead of running the doInBackround
function. It can be useful when you have the same onPreExecute and onPostExecute but several runnables.
I guess the advantage over Thread.execute
or an Executor
is exactly calling onPreExecute
and onPostExecute
before and after.
Solution 2
There are a couple of fundamental differences between
static void execute(Runnable)
and
AsyncTask execute(Params...)
- Background task is defined in Runnable instead of implementing
doInBackground
- The
Runnable
-task is not using the internal thread communication mechanism of theAsyncTask
. Hence, neitheronPreExecute
noronPostExecute
are called. - The latter is available on all platforms, whereas the first was added in API level 11.
The advantage of using execute(Runnable)
is that the task can be executed on a worker thread of the internal thread pool, i.e. no new thread has to be created.
Solution 3
As far as i figure it's like AsyncTask
class but AsynchTask
only runs once, but with this class it provides two things-:
- It loops so it benefits, if you want a task to run multiple time like checking for continuous data on a web service.
- It fixed the running time of a task with Thread.sleep, so if a task finished
earlier it will fix the time of this task by
Thread.wait()
.
Solution 4
@Alex makes a very good point. Suppose the you have a lot of methods, M1()
, M2()
, and so on that you wish to execute. Suppose that before executing any of them you need to execute method Before()
and after you need to execute method After()
.
ie, the sequence of methods goes:
Before();
M1();
After();
Or
Before();
M2();
After();
By putting Before()
in onPreExecute
and After()
in onPostExecute
you can achieve that sequence. By making M
a runnable, you can then achieve:
Before();
WhateverRunnableYouWant();
After();
With the Runnable
in a background, non-UI, thread, as per your code.
Pravy
Updated on June 04, 2022Comments
-
Pravy almost 2 years
I have a class
AsycnIntegerCounter
which extendsAsyncTask
, withdoInBackground()
andonPostExecute()
overridden in the same. From my main thread, I am able to create a runnable object and execute it using theAsycnIntegerCounter
's static execute method.AsycnIntegerCounter.execute(Runnable)
Can anyone help me in understanding what exactly happens when we execute a runnable using
AsycnIntegerCounter
(i.e) usingAsycnTask
object.When this can be used ? and what is the advantage rather than running using a Thread object?
Code Sample:
AsycnIntegerCounter integerCounter1 = new AsycnIntegerCounter(next,0); AsycnIntegerCounter.execute(new Runnable() { @Override public void run() { int i = 100; while(i<=105){ i++; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } });
-
Pravy almost 11 yearsThanks for replying Alex, but I could not understand following part "It can be useful when you have the same onPreExecute and onPostExecute but several runnables." Also when I execute the runnable using the execute() control does not go to onPostExecute() of the AsycnIntegerCounter class after the runnable has finished its task, is this the expected behavior?
-
Pravy almost 11 yearsThanks Neil, but when I execute the runnable using the execute method, the control never goes to onPostExecute() or onPreExecute(). Should I add any code to call them in this case??
-
Neil Townsend almost 11 yearsCould you post
AsycnIntegerCounter
please to help answer that, thanks. -
Pierre about 4 yearsAlso to note that
AsyncTask.execute(...);
uses the default executor which is an instance ofSerialExecutor