繁体   English   中英

Android,获取当前线程ID的两种方法有什么区别?

[英]Android, what is the difference between two ways of getting current thread Id?

当我记录下面的两个线程ID时,它们是不同的。

long threadId = Thread.currentThread().getId();
long threadId2 = android.os.Process.myTid();

但是从Android文档来看,它们是完全相同的:

/**
* Returns the Thread of the caller, that is, the current Thread.
*/
public static native Thread currentThread();

因此,我假设第一行将返回调用者线程的ID。

然后这个

/**
     * Returns the identifier of the calling thread, which be used with
     * {@link #setThreadPriority(int, int)}.
     */
    public static final int myTid() {
        return Os.gettid();
    }

从注释中看,它似乎也是调用者线程的ID(尽管它们使用单​​词“ calling”)。

他们应该是一样的,还是我错过了什么? 谢谢。

ThreadId和Tid不同。

ThreadId更像是pid,而Tid则更复杂。

[更正:] ThreadId 不是 pid,它实际上是++Thread.count的值

在此处查看详细信息: pid和tid 之间的区别以及此处: Android中Thread.currentThread()。getId()和Process.myTid()之间的区别

Thread.currentThread()。getId():返回调用者线程的标识符,即当前线程。 android.os.Process.myTid():返回调用线程的标识符,该标识符与setThreadPriority(int,int)一起使用。 此处根据Linux优先级设置线程的优先级。

参数tid要更改的线程/进程的标识符。 优先级Linux优先级,从-20(最高调度优先级)到19(最低调度优先级)。

供进一步参考

http://developer.android.com/reference/android/os/Process.html#myTid()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM