Reputation: 1851
I am trying to get a threads id. I use this approach :
But the recommended way always returns 0.
This line does,what it should
Thread.currentThread.getID().
Which is the right one ?
Upvotes: 3
Views: 5550
Reputation: 95578
These are 2 different things.
Thread.currentThread().getID();
returns the thread ID of the Thread
within the Virtual machine as long
.
android.os.Process.myTid();
returns the "Thread identifier" as an int
. "Thread identifier" is a value that can be used to control processes at the Linux level and has nothing to do with the Virtual machine. This value is used in calls to android.os.Process.setThreadPriority()
and android.os.Process.getThreadPriority()
.
Upvotes: 0
Reputation: 6276
Thread.currentThread().getId();
According to Documentation
Returns the thread's identifier. The ID is a positive long generated on thread creation, is unique to the thread, and doesn't change during the lifetime of the thread; the ID may be reused after the thread has been terminated.
android.os.Process.myTid();
According to Documentation
Returns the identifier of the calling thread, which be used with setThreadPriority(int, int).
According to my understanding, Thread.currentThread().getId();
will return the Thread Id by which a user or running program can identify a thread while on the other hand android.os.Process.myTid();
will provide the id by which the processor or running machine identifies a thread and this will be unique and I do not think the same id by this can be assigned to any other thread once it is dead.
Upvotes: 6