EyalBellisha
EyalBellisha

Reputation: 1914

Can I tell which thread called my service method?

Let's say I have a service that spawns 2 threads. These 2 threads can access any of the service's methods. Is there any way I can tell (within a method) which of the threads had called it?

I know such a mechanism exists for processes in Android: I can use getCallingUid for instance, but is there something similar for threads as well?

Thank you

Upvotes: 1

Views: 121

Answers (3)

Robert Estivill
Robert Estivill

Reputation: 12477

What if you pass in the Thread.getId ?

Upvotes: 0

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

Thread.currentThread() will always deliver current thread.

Upvotes: 1

Mikita Belahlazau
Mikita Belahlazau

Reputation: 15434

You can use currentThread method to get thread and use getName to get it's name:

String name = Thread.currentThread().getName();
Log.d("Service", "Called from " + name + " thread");

When you create create thread you can specify it's name in constructor. See Thread

Upvotes: 3

Related Questions