Reputation: 1914
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
Reputation: 12367
Thread.currentThread() will always deliver current thread.
Upvotes: 1
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