Android:Process.myTid()VS Thread.currentThread().getId() [英] Android: Process.myTid() VS Thread.currentThread().getId()
问题描述
我有一个简单的Activity
调用AsyncTask
,所以我打印一些有关Proces
和Thread
的ID:
I have simple Activity
that calls AsyncTask
, so I print some id's regarding Proces
and Thread
:
From onCreate android.os.Process.myUid(): 10137
From onCreate android.os.Process.myPid(): 29776
From onCreate android.os.Process.myTid(): 29776
From onCreate Thread.currentThread().getId(): 1
/****************************************************************/
From Async doInBackground android.os.Process.myUid(): 10137
From Async doInBackground android.os.Process.myPid(): 29776
From Async doInBackground android.os.Process.myTid(): 30426
From Async doInBackground Thread.currentThread().getId(): 12556
-
Uid
之所以相同,是因为其应用特定的沙箱 - 与
Pid
类似:每个应用都是一个Process
- 第三行与
Pid
相同,因为它是UIThread
,而在基于Linux的Android操作系统中,我们知道关于Process
的问题实际上是Thread
等...而在ThreadId
是不同的,因为AsyncTask
在不同的Thread
而不是UIThread
上运行
onCreate
中的Uid
is same because its app-specific sandbox- Similar with
Pid
: Each app is oneProcess
- 3rd line in
onCreate
same asPid
because it's theUIThread
and in Android OS as based on Linux we know that issue regardingProcess
is actuallyThread
etc... And in theAsync
theThreadId
is different becauseAsyncTask
runs on differentThread
rather then theUIThread
我要努力理解的是Thread.currentThread().getId()
.我期望在相同的执行环境中获得与Thread.currentThread().getId()
相同的ID.例如对于onCreate
,我希望第3,4行是相同的(29776),对于Async
,我希望第3,4行是相同的(30426).这是怎么回事?
The thing I'm struggling to understand is Thread.currentThread().getId()
. What I expect is to get same id as Thread.currentThread().getId()
for the same execution environment. e.g. for onCreate
I want lines 3,4 to be same (29776), and for Async
I expect lines 3,4 to be the same (30426). What is going on here?
谢谢
推荐答案
OP提出了一个非常有趣的问题,我决定进行挖掘(喜欢开源).
Very interesting question by the OP and I decided to dig (love open source).
简短的答案是:它们之所以不同,是因为它们不同,因为它们本来就不会一样.
The short answer is: they're different because they're different, because they were never meant to be the same.
-
Process.myTid()
是linux线程ID -
Thread.getId()
是一个简单的连续long
数字.
Process.myTid()
is the linux thread IDThread.getId()
is a simple sequentiallong
number.
但是简短的答案很无聊,因此让我们探究答案的来源(答案中指向相关源代码的链接).
But the short answer is boring, so let's explore where the answer comes from (links in the answer points to the relevant source codes).
在 Os.gettid()
本身会在
In Process.myTid()
, you'll see that is simply calls from Os.gettid()
that in itself calls a native method on Libcore
for that method is below:
public static int gettid() { return Libcore.os.gettid(); }
此外,Os.gettid();
的文档,您还会找到 Linux程序员手册的链接
furthermore the docs for Os.gettid();
you'll find a link to Linux Programmer's Manual
gettid()返回调用者的线程ID(TID).在单线程中 进程,线程ID等于进程ID(PID,返回 通过getpid(2)).在多线程进程中,所有线程都具有相同的 PID,但每个都有一个唯一的TID.
gettid() returns the caller's thread ID (TID). In a single-threaded process, the thread ID is equal to the process ID (PID, as returned by getpid(2)). In a multithreaded process, all threads have the same PID, but each one has a unique TID.
- 这意味着
Process.myTid()
返回Linux内核给出的线程ID. - That means,
Process.myTid()
returns the thread ID as given by the Linux kernel.
另一方面, tid = nextThreadId();
.然后是这个难题的最后一部分,下面是
On the other hand Thread.getId()
is simply returning a long
. This long is assigned during init(...)
as tid = nextThreadId();
. Then the last piece of this puzzle, below is the code for nextThreadId()
/* For generating thread ID */
private static long threadSeqNumber;
private static synchronized long More ...nextThreadID() {
return ++threadSeqNumber;
}
- 这意味着,
Thread.getId()
仅仅是一个"java层"静态对象,可以为每个线程自动递增. - That means,
Thread.getId()
is simply a "java layer" static long being auto-increment for each thread.
这篇关于Android:Process.myTid()VS Thread.currentThread().getId()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!