当我尝试在线程中使用它时,此代码抛出“给定线程不存在”异常:
android.os.Process.getThreadPriority((int) Thread.currentThread().getId()));
如果我尝试使用Process.setThreadPriority,则使用java类Thread ID。我也注意到这与调试器中显示的线程ID不匹配。如何获取Android特定的线程ID,以便使用此API?
答案 0 :(得分:59)
android.os.Process.getThreadPriority(android.os.Process.myTid());
供进一步参考
http://developer.android.com/reference/android/os/Process.html#myTid()
答案 1 :(得分:11)
我们正在使用线程。我们还想记录线程细节以解决与线程相关的问题。创建一个Utils类,如下所示,并使用它来记录线程签名。
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);
}
}