我正在尝试获取线程 ID。 我使用这种方法:
但推荐的方式总是返回 0。
这条线做了什么,它应该做什么
Thread.currentThread.getID().
哪个是正确的?
最佳答案
Thread.currentThread().getId();
根据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();
根据Documentation
Returns the identifier of the calling thread, which be used with setThreadPriority(int, int).
根据我的理解,Thread.currentThread().getId();
将返回线程 ID,用户或正在运行的程序可以通过该 ID 识别线程,而另一方面 android .os.Process.myTid();
将提供处理器或正在运行的机器用来标识线程的 id,这将是唯一的,我不认为可以将与此相同的 id 分配给任何其他线程一次它死了。
关于java - Android - 获取线程 ID - 使用此 SO 方法始终返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660895/