How to get Android Thread ID?

前端 未结 2 1053
伪装坚强ぢ
伪装坚强ぢ 2021-02-01 13:27

This code throws a \"Given thread does not exist\" exception when I try to use it in a thread:

android.os.Process.getThreadPriority((int) Thread.currentThread().         


        
相关标签:
2条回答
  • 2021-02-01 14:11

    While we are working with threads. We also want to log thread details to solve thread related problem. Create one Utils class as below and use it to log thread signature.

    public class Utils 
    {
       public static long getThreadId()
       {
          Thread t = Thread.currentThread();
          return t.getId();
       }
    
       public static String getThreadSignature()
       {
          Thread t = Thread.currentThread();
          long l = t.getId();
          String name = t.getName();
          long p = t.getPriority();
          String gname = t.getThreadGroup().getName();
          return (name 
                + ":(id)" + l 
                + ":(priority)" + p
                + ":(group)" + gname);
       }
    
       public static void logThreadSignature()
       {
          Log.d("ThreadUtils", getThreadSignature());
       }
    
       public static void sleepForInSecs(int secs)
       {
          try
          {
             Thread.sleep(secs * 1000);
          }
          catch(InterruptedException x)
          {
             throw new RuntimeException("interrupted",x);
          }
       }
    

    Reference : www.androidbook.com

    0 讨论(0)
  • 2021-02-01 14:22
    android.os.Process.getThreadPriority(android.os.Process.myTid());
    

    For further reference

    http://developer.android.com/reference/android/os/Process.html#myTid()

    0 讨论(0)
提交回复
热议问题