4

As known

myTid() - Returns the identifier of the calling thread, which be used with setThreadPriority(int, int).

But I found Thread.currentThread().getId() does not equal to Process.myTid(). So, my guess the former is a JVM edition thread id and the latter is Linux edition thread id.

Am I right? If so, why does java make its own thread id and not use Linux thread id?

UPDATE:

After further research and reading the source code of android, I have new realization:

Process.myTid() is a platform(OS) related operation, and so does Process.setThreadPriority(), in native level source of android, they all invoke a system call to realize the target.

But java is a platform independent language, java doesn't force the host os need a "tid" or a getTid() method, since another os may identify its thread by a string key(just a example, :)). Then java identify its thread with its own way, assign a unique thread id in java scope, if java provide a static api like Process.setThreadPriority(), then the java scope id definitely will be one parameter, but we needn't since we can do this by call a Thread object method setPriority(int priority).

Any comment is welcome.

Updated:

The answers all is right. But fadden's comment make me more clear. Thank you all.

ieatbyte
  • 133
  • 2
  • 6
  • @EJP `Process.myPid()` returns the process id. `Process.myTid()` is something different. – Eng.Fouad Nov 11 '13 at 04:02
  • 1
    Linux threads have a process ID (a/k/a thread group ID) and a thread ID. Dalvik threads have an internal 16-bit thread ID, necessary because Dalvik wants a short thread identifier and can't assume the OS will provide it (see: Mac OS X). java.lang.Thread has its own ID, which *could* be the same as one of the others, but isn't. So... take your pick. – fadden Nov 11 '13 at 18:58

2 Answers2

0

Thread.getId() : return id;

where id is assigned:

Thread.create : id = ++Thread.count

so, Process.myTid() is 'System tid', and Thread.getId() is 'Java thread id'

afpro
  • 1,103
  • 7
  • 12
0

I believe that there are two types of threads in a Java environment:

  • Native Thread: which is managed by the hosted OS.
  • Java Thread: which is managed by the JVM and is communicated with the native thread.
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417