Java – Android – Get thread ID – Use this SO method to always return 0

Android – Get thread ID – Use this SO method to always return 0… here is a solution to the problem.

Android – Get thread ID – Use this SO method to always return 0

I’m trying to get the thread ID.
I use this method:

How to get Android Thread ID?

But the recommended way always returns 0.

What this line does, what it should do

Thread.currentThread.getID().

Which is correct?

Solution

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).

As I understand it, Thread.currentThread().getId(); A thread ID is returned, by which the user or running program can identify the thread, while android.os, on the other hand. Process.myTid(); The ID that the processor or running machine uses to identify the thread will be provided, it will be unique and I don’t think it’s possible to assign the same id to any other thread once it’s dead.

Related Problems and Solutions