Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Android Thread ID?

Tags:

android

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?

like image 386
Jonathan Avatar asked Nov 30 '11 20:11

Jonathan


People also ask

How do I find my current thread ID?

In the run() method, we use the currentThread(). getName() method to get the name of the current thread that has invoked the run() method. We use the currentThread(). getId() method to get the id of the current thread that has invoked the run() method.

What is an android thread?

A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority.

What is the use of thread ID?

The thread ID is used by the operating system to identify processes and threads. The thread id is unique globally but the users can capture the thread handle of the process thread through this id.

Is threads available on Android?

Threading in Android In Android, you can categorize all threading components into two basic categories: Threads that are attached to an activity/fragment: These threads are tied to the lifecycle of the activity/fragment and are terminated as soon as the activity/fragment is destroyed.


2 Answers

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

For further reference

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

like image 151
havexz Avatar answered Sep 25 '22 12:09

havexz


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

like image 43
Vijay Vankhede Avatar answered Sep 25 '22 12:09

Vijay Vankhede