How to get Android Thread ID?

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().getId()));

Ditto if I try to use Process.setThreadPriority, using the java class Thread id. I’ve also noticed that this does not match the thread id displayed in the debugger. How do I get the Android specific thread id, in order to use this API?

Android get contact id from thread id

I’m working on a simple sms app and I’m using the code below to get the thread id when loading my threads list but I can’t figure out how to get the contact id using the thread id. I’m root and using

Android thread id check

Google’s gtalksms for Android uses a Handler inside a Service. When it starts the handler thread it stores the thread id and then in the Handler has the following check: if (Thread.currentThread().get

How can you get the Linux thread Id of a std::thread()

I was playing with std::thread and I was wondering how is it possible to get the thread id of a new std::thread(), I am not talking about std::thread::id but rather the OS Id given to the thread ( you

How to get Android application id?

In Android, how do I get the application’s id programatically (or by some other method), and how can I communicate with other applications using that id?

How to get message thread URL knowing the thread id?

There is How can I construct a link to view a message on facebook.com if I have the message id question, but it is unanswered. What if I got the id of the thread using /me/inbox API endpoint and need

Get message thread id or _id from onReceive android

I have tried to get a message id which is saved in phone. But I failed to get it. My existing code is here @Override public void onReceive(Context context, Intent intent) { String address = ; Strin

Java: How to get thread id from a thread pool?

I have a fixed thread pool that I submit tasks to (limited to 5 threads). How can I find out which one of those 5 threads executes my task (something like thread #3 of 5 is doing this task)? Executo

how to get thread id of a pthread in linux c program?

In linux c program, how to print thread id of a thread created by pthread library? for ex: we can get pid of a process by getpid()

Thread ID From thread name

How can I get the thread ID of a Win32 thread from the thread name?

Get a thread by Id

I worked in JMX java and I get all the thread IDs by using the getAllThreadIds () method of the interface ThreadMXBean but I need a way to kill the thread of a given ID. for example: ThreadMXBean tbe

Answers

android.os.Process.getThreadPriority(android.os.Process.myTid());

For further reference

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

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