Android:Process.myTid()VS Thread.currentThread().getId() [英] Android: Process.myTid() VS Thread.currentThread().getId()

查看:105
本文介绍了Android:Process.myTid()VS Thread.currentThread().getId()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Activity调用AsyncTask,所以我打印一些有关ProcesThread的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

  1. Uid之所以相同,是因为其应用特定的沙箱
  2. Pid类似:每个应用都是一个 Process
  3. onCreate中的
  4. 第三行与Pid相同,因为它是UIThread,而在基于Linux的Android操作系统中,我们知道关于Process的问题实际上是Thread等...而在 ThreadId是不同的,因为AsyncTask在不同的Thread而不是UIThread
  5. 上运行
  1. Uid is same because its app-specific sandbox
  2. Similar with Pid: Each app is one Process
  3. 3rd line in onCreate same as Pid because it's the UIThread and in Android OS as based on Linux we know that issue regarding Process is actually Thread etc... And in the Async the ThreadId is different because AsyncTask runs on different Thread rather then the UIThread

我要努力理解的是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 ID
  • Thread.getId() is a simple sequential long 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.

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆