FileDocCategorySizeDatePackage
HandlerThread.javaAPI DocAndroid 1.5 API2823Wed May 06 22:41:56 BST 2009android.os

HandlerThread

public class HandlerThread extends Thread
Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

Fields Summary
private int
mPriority
private int
mTid
private Looper
mLooper
Constructors Summary
public HandlerThread(String name)


       
        super(name);
        mPriority = Process.THREAD_PRIORITY_DEFAULT;
    
public HandlerThread(String name, int priority)
Constructs a HandlerThread.

param
name
param
priority The priority to run the thread at. The value supplied must be from {@link android.os.Process} and not from java.lang.Thread.

        super(name);
        mPriority = priority;
    
Methods Summary
public LoopergetLooper()
This method returns the Looper associated with this thread. If this thread not been started or for any reason is isAlive() returns false, this method will return null. If this thread has been started, this method will blocked until the looper has been initialized.

return
The looper.

        if (!isAlive()) {
            return null;
        }
        
        // If the thread has been started, wait until the looper has been created.
        synchronized (this) {
            while (isAlive() && mLooper == null) {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            }
        }
        return mLooper;
    
public intgetThreadId()
Returns the identifier of this thread. See Process.myTid().

        return mTid;
    
protected voidonLooperPrepared()
Call back method that can be explicitly over ridden if needed to execute some setup before Looper loops.

    
public voidrun()

        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            Process.setThreadPriority(mPriority);
            notifyAll();
        }
        onLooperPrepared();
        Looper.loop();
        mTid = -1;