Executing a runnable in Asynctask

11,097

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...)
  1. Background task is defined in Runnable instead of implementing doInBackground
  2. The Runnable-task is not using the internal thread communication mechanism of the AsyncTask. Hence, neither onPreExecute nor onPostExecute are called.
  3. 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-:

  1. It loops so it benefits, if you want a task to run multiple time like checking for continuous data on a web service.
  2. 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.

Share:
11,097
Pravy
Author by

Pravy

Updated on June 04, 2022

Comments

  • Pravy
    Pravy almost 2 years

    I have a class AsycnIntegerCounter which extends AsyncTask, with doInBackground() and onPostExecute() overridden in the same. From my main thread, I am able to create a runnable object and execute it using the AsycnIntegerCounter'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) using AsycnTask 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
    Pravy almost 11 years
    Thanks 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
    Pravy almost 11 years
    Thanks 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
    Neil Townsend almost 11 years
    Could you post AsycnIntegerCounter please to help answer that, thanks.
  • Pierre
    Pierre about 4 years
    Also to note that AsyncTask.execute(...); uses the default executor which is an instance of SerialExecutor