Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name the Thread of an AsyncTask

Is it possible to give a name to an AsyncTask's background thread, as for normal Threads in Java:

Thread(Runnable target, String name)  

I have seen the code of AsyncTask and the default constructor is just give a name by default

private static final ThreadFactory sThreadFactory = new ThreadFactory() {     private final AtomicInteger mCount = new AtomicInteger(1);      public Thread newThread(Runnable r) {         return new Thread(r, "AsyncTask #" + mCount.getAndIncrement());     } }; 

I want to do that, as when I debug I want to know which Asynctask is accessing a method in a helper class.

like image 935
Dayerman Avatar asked Oct 07 '11 07:10

Dayerman


People also ask

How many threads are there in AsyncTask?

In newer Android versions, 5 threads are create by default, and the ThreadPoolExecutor will attempt to run the AsyncTask s on these 5 threads. If you create more than 5 AsyncTask s, it may either queue them or create new threads (but only up to 128).

Is AsyncTask a thread?

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

In which thread AsyncTask function will execute?

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What is the use of AsyncTask in Android?

Android AsyncTask is an abstract class provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive. Android application runs on a single thread when launched.


1 Answers

Try calling this from inside doInBackground():

Thread.currentThread().setName("some custom name"); 
like image 126
Felix Avatar answered Oct 12 '22 14:10

Felix