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

查看:17
本文介绍了安卓: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 中的第 3 行与 Pid 相同,因为它是 UIThread 并且在基于 Linux 的 Android 操作系统中我们知道关于 Process 实际上是 Thread 等等......而在 Async 中,ThreadId 是不同的,因为 AsyncTask> 运行在不同的 Thread 而不是 UIThread
  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).

Process.myTid(),你会看到它只是来自 Os.gettid() 本身调用 Libcore 该方法是下面:

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.
    • 另一方面 Thread.getId() 只是返回一个 long.这个长在 init(...) 期间被分配为 tid = nextThreadId();.那么这个拼图的最后一块,下面是 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.
        • 这篇关于安卓:Process.myTid() VS Thread.currentThread().getId()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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