2011-11-30 12 views
29

Questo codice genera un "filo Dato non esiste" eccezione quando cerco di usarlo in un thread:Come ottenere l'ID discussione Android?

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

Idem se si tenta di utilizzare Process.setThreadPriority, usando l'id classe Java Discussione. Ho anche notato che questo non corrisponde all'ID del thread visualizzato nel debugger. Come ottengo l'ID del thread specifico per Android, per poter utilizzare questa API?

+2

Avete controllato se 'Process.myTid()' viene fatto uso per voi? – harism

risposta

9

Mentre stiamo lavorando con fili. Vogliamo anche registrare i dettagli del thread per risolvere il problema relativo al thread. Crea una classe Utils come sotto e usala per registrare la firma del thread.

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); 
     } 
    } 

Riferimento: www.androidbook.com

Problemi correlati