java - Android - 获取线程 ID - 使用此 SO 方法始终返回 0

标签 java android multithreading

我正在尝试获取线程 ID。 我使用这种方法:

How to get Android Thread 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/

相关文章:

java - Maven POM 编辑器 : Dependency Graph missing

android - 重叠的 mp4 文件的无缝播放

multithreading - 由于异步,在 WebApi 中使用 HttpContext.Current 是危险的

java - 多线程快速排序比预期慢很多

java - 在线程上设置中断标志

java - 任何可以限制内存缓存的内存使用的 Java 缓存,而不仅仅是实例计数?

java - 在 Java 中使用线程安全集合时,处理并发问题的最佳方法是什么?

java - Android 后台服务没有停止/终止

Android SharedPreferences 或 SQLite 存储

java - 如何向 Java 线程传递参数?