HandlerThread.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
19/**
20 * Handy class for starting a new thread that has a looper. The looper can then be
21 * used to create handler classes. Note that start() must still be called.
22 */
23public class HandlerThread extends Thread {
24    private int mPriority;
25    private int mTid = -1;
26    private Looper mLooper;
27
28    public HandlerThread(String name) {
29        super(name);
30        mPriority = Process.THREAD_PRIORITY_DEFAULT;
31    }
32
33    /**
34     * Constructs a HandlerThread.
35     * @param name
36     * @param priority The priority to run the thread at. The value supplied must be from
37     * {@link android.os.Process} and not from java.lang.Thread.
38     */
39    public HandlerThread(String name, int priority) {
40        super(name);
41        mPriority = priority;
42    }
43
44    /**
45     * Call back method that can be explicitly over ridden if needed to execute some
46     * setup before Looper loops.
47     */
48    protected void onLooperPrepared() {
49    }
50
51    public void run() {
52        mTid = Process.myTid();
53        Looper.prepare();
54        synchronized (this) {
55            mLooper = Looper.myLooper();
56            Process.setThreadPriority(mPriority);
57            notifyAll();
58        }
59        onLooperPrepared();
60        Looper.loop();
61        mTid = -1;
62    }
63
64    /**
65     * This method returns the Looper associated with this thread. If this thread not been started
66     * or for any reason is isAlive() returns false, this method will return null. If this thread
67     * has been started, this method will blocked until the looper has been initialized.
68     * @return The looper.
69     */
70    public Looper getLooper() {
71        if (!isAlive()) {
72            return null;
73        }
74
75        // If the thread has been started, wait until the looper has been created.
76        synchronized (this) {
77            while (isAlive() && mLooper == null) {
78                try {
79                    wait();
80                } catch (InterruptedException e) {
81                }
82            }
83        }
84        return mLooper;
85    }
86
87    /**
88     * Returns the identifier of this thread. See Process.myTid().
89     */
90    public int getThreadId() {
91        return mTid;
92    }
93}
94