Android中:
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
//在一个单线程的进程中,gettid()返回的线程id和getpid()返回的进程id相同
Log.i("test", "_java thread id_:" + Thread.currentThread().getId());
Log.i("test", "_java pid_:" + android.os.Process.myPid());
Log.i("test", "_java tid_:" + android.os.Process.myTid());
Log.i("test", "_java uid_:" + android.os.Process.myUid());
}
}).start();
Log.i("test", "java thread id:" + Thread.currentThread().getId());
Log.i("test", "java pid:" + android.os.Process.myPid());
Log.i("test", "java tid:" + android.os.Process.myTid());
Log.i("test", "java uid:" + android.os.Process.myUid());
JNI中:
LOGD("tid: %d\n", gettid());
LOGD("pid: %d\n", getpid());
输出Log:
01-11 15:52:04.309: I/test(15850): java thread id:1
01-11 15:52:04.309: I/test(15850): java pid:15850
01-11 15:52:04.309: I/test(15850): java tid:15850
01-11 15:52:04.309: I/test(15850): java uid:10117
01-11 15:52:04.309: I/test(15850): hello world jni
01-11 15:52:04.309: I/test(15850): _java thread id_:1759
01-11 15:52:04.309: I/test(15850): _java pid_:15850
01-11 15:52:04.309: I/test(15850): _java tid_:16061
01-11 15:52:04.309: I/test(15850): _java uid_:10117
解释:
首先android中的UI线程是映射到Linux中进程的的一个posix线程。
在Linux中进程若只有一个posix线程,则进程和线程id是一致的。
Thread.currentThread().getId()是获得一个java语言级的线程id。而android.os.Process.myPid()和android.os.Process.myTid()及gettid()和getpid()是获得一个系统的进程或者线程id。