Home | History | Annotate | Download | only in os
      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 
     17 package 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  */
     23 public class HandlerThread extends Thread {
     24     int mPriority;
     25     int mTid = -1;
     26     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 overridden 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             notifyAll();
     57         }
     58         Process.setThreadPriority(mPriority);
     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 block 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      * Ask the currently running looper to quit.  If the thread has not
     89      * been started or has finished (that is if {@link #getLooper} returns
     90      * null), then false is returned.  Otherwise the looper is asked to
     91      * quit and true is returned.
     92      */
     93     public boolean quit() {
     94         Looper looper = getLooper();
     95         if (looper != null) {
     96             looper.quit();
     97             return true;
     98         }
     99         return false;
    100     }
    101 
    102     /**
    103      * Returns the identifier of this thread. See Process.myTid().
    104      */
    105     public int getThreadId() {
    106         return mTid;
    107     }
    108 }
    109