1

I have created the following background thread:

public class loadSomeStuff extends AsyncTask <String, Integer, String>
{       
    @Override
    protected String doInBackground(String... params) {
        Intent i = new Intent("com.example.nadir_services.himan.class");           startActivity(i);
        return null;
    }
}

I also created a button, in which I call this thread:

new loadSomeStuff().execute("");

So, my question is, every time I press this button, will it create a new thread? If so, why is that bad? I mean, what will happen ?

And a side question, I noticed that this is called a "background thread". Does that mean there is another way to create a new thread ? I only know of this way.

madhan kumar
  • 1,560
  • 2
  • 26
  • 36
user3134565
  • 965
  • 1
  • 7
  • 14

3 Answers3

5

no, AsyncTask is constructed with ThreadPool of 4 threads and by default the system chooses one of those threads to run on.

In general, if you want to avoid memory issues, you should initialise the AsyncTask outside from the onClick and execute it from there instead of creating a new one every time like this:

private loadingTask = new LoadSomeStuff();

@Override
public void onClick(View v) {
    loadingTask.execute("");
}

edit, for future reference, please have a look at the source code for AsyncTask

thepoosh
  • 12,497
  • 15
  • 73
  • 132
  • 1
    can you explain what this means, i just don't think i have the background to understand what you said. – user3134565 Feb 11 '14 at 13:21
  • basically, what happens is that all asynctasks are run by a manager on dedicated threads. those threads are being reused and when the app requires more threads, more are created to make that happen. then, `Runnable` objects are posted into those threads and return their result – thepoosh Feb 11 '14 at 13:46
1

will it create a new thread?

No, AsyncTask is added to a pool of threads (initially 4, up to (number of CPU * 2) + 1) which will be used to run your background Task.

why is that bad?

You should not create a new instance of your AsyncTask every time you click the button.. You should declare your instance of AsyncTask outside onClick and only launch it from within that method.

does that mean there is another way to create a new thread ?

Yes. AsyncTask is there to provide you with practical callback methods to know when the task is finished (onPostExecute), execute code before the task runs (onPreExecute), post progress ... The other way is creating a Worker Thread.

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • 1
    it's not max 4, but initially 4, max is number of cores * 2 as seen [here](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/AsyncTask.java#182) – thepoosh Feb 11 '14 at 13:43
  • @thepoosh, thank you, you are absolutely right. Isn't true, though, that the pool will only keep 4 (or 5, not sure) of those alive and the rest will be killed after task execution ? Will edit the answer once that is clarified. – 2Dee Feb 11 '14 at 13:47
  • 1
    no, they accumulate until the AsyncTask goes through GC, every new AsyncTask object is initialised with 4 threads in the executer by default and they get reused – thepoosh Feb 11 '14 at 13:49
  • Thanks for the details, answer edited and +1 on yours for taking the time to correct mine ;) – 2Dee Feb 11 '14 at 13:53
0

Its called the "background thread" because there is a "main" thread which is the UI Thread for Android. This basically controls/operates on what is happening in front of the user.

When you call an async task - background thread - means the operation associated with your task is broken down into what is happening in operation and meanwhile what is happening in foreground for the User.

To understand the difference exactly, please have a look at:
http://developer.android.com/guide/components/processes-and-threads.html
http://developer.android.com/training/multiple-threads/communicate-ui.html
http://developer.android.com/reference/android/os/AsyncTask.html

What is the Android UiThread (UI thread)

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • Also, http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html#.UvojsfmSzJc – Pararth Feb 11 '14 at 13:26
  • but if i press the button more than once, will it create a new thread each time, or will it execute the same thread ? – user3134565 Feb 11 '14 at 13:28
  • Ideally, No. Thats the way Async Tasks are to be utilized - that once you click the button, you can show a progress dialog - start and stop that in onPre and onPost Execute methods, while you carry out your operation in background. But the behaviour of async tasks has varied over the O.S. versions. So please check "Order of Execution on: >http://developer.android.com/reference/android/os/AsyncTask.html – Pararth Feb 11 '14 at 13:33