Rovdjuret
Rovdjuret

Reputation: 1528

Unsure if its running on own thread

Im unsure if my service is running on a own thread.

Im doing this in the onCreate method in my service:

@Override
public void onCreate() {
    HandlerThread thread = new HandlerThread("ServiceStartArguments");
    thread.start();
}

I can see the thread being created when debugging but still I'm unsure as I see people go for something like this:

handler=new Handler();
Runnable r=new Runnable()
{
   public void run() 
   {
       tv.append("Hello World");            
   }
};
handler.postDelayed(r, 1000);

How, what why? Please explain, I don't get it!

This makes me confused also..

I/Choreographer﹕ Skipped 52 frames!  The application may be doing too much work on its main thread.

Upvotes: 0

Views: 49

Answers (2)

Steve M
Steve M

Reputation: 9784

Anything that's executed in the run() method of HandlerThread is running on the thread you are starting with thread.start() in onCreate(). Anything else in the Service is running on the UI thread by default (unless you are using IntentService, then onHandleIntent() is automatically running in a non-UI thread).

The pattern you posted is probably demonstrating how to update the UI on the UI thread from a different thread.

Upvotes: 2

Sadegh
Sadegh

Reputation: 2669

Use this to check for thread's id:

long myThreadId = Thread.currentThread().getId();
Log.d("thread debuging", "Thread id is: " + myThreadId);

Alternatively you can also use these to set a name for your threads:

Thread.currentThread().setName("A_NAME_FOR_THIS_THREAD");
String threadName = Thread.currentThread().getName();

Upvotes: 1

Related Questions