Android中HandlerThread类的解析

本文介绍了Android中如何在子线程使用Handler处理消息。通过Looper.prepare()和Looper.loop()函数创建Looper对象,并利用HandlerThread类解决子线程中Looper对象的创建问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android应用中的消息循环由Looper和Handler配合完成,Looper类用于封装消息循环,类中有个MessageQueue消息队列;Handler类封装了消息投递和消息处理等功能。

系统默认情况下只有主线程(即UI线程)绑定Looper对象,因此在主线程中可以直接创建Handler的实例,但是在子线程中就不能直接new出Handler的实例了,因为子线程默认并没有Looper对象,此时会抛出RuntimeException异常:

浏览下Handler的默认构造函数就一目了然了:

    public Handler() {
        if (FIND_POTENTIAL_LEAKS) {
            final Class<? extends Handler> klass = getClass();
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                    (klass.getModifiers() & Modifier.STATIC) == 0) {
                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                    klass.getCanonicalName());
            }
        }

        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = null;
    }

如果需要在子线程中使用Handler类,首先需要创建Looper类实例,这时可以通过Looper.prepare()和Looper.loop()函数来实现的。阅读Framework层源码发现,Android为我们提供了一个HandlerThread类,该类继承Thread类,并使用上面两个函数创建Looper对象,而且使用wait/notifyAll解决了多线程中子线程1获取子线程2的Looper对象为空的问题。HandlerThread类完整代码如下:

/**
 * 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.
 */
public class HandlerThread extends Thread {
	
	private int mPriority; // 线程优先级
	
	private int mTid = -1; // 线程ID
	
	private Looper mLooper; // 我们需要的Looper对象

	public HandlerThread(String name) {
		super(name);
		mPriority = Process.THREAD_PRIORITY_DEFAULT;
	}

	/**
	 * 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.
	 */
	public HandlerThread(String name, int priority) {
		super(name);
		mPriority = priority;
	}

	/**
	 * 在Looper.loop()之前执行动作的回调函数
	 */
	protected void onLooperPrepared() {
	}

	public void run() {
		mTid = Process.myTid();
		Looper.prepare(); // 创建本线程的Looper对象
		
		synchronized (this) {
			mLooper = Looper.myLooper();
			notifyAll(); //通知所有等待该线程Looper对象的其他子线程,本线程的Looper对象已就绪
		}
		
		Process.setThreadPriority(mPriority);
		onLooperPrepared(); //回调函数
		
		Looper.loop(); //开始消息队列循环
		mTid = -1;
	}

	/**
	 * 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 block until the looper has been initialized.
	 * 
	 * @return The looper.
	 */
	public Looper getLooper() {
		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(); //Looper对象未创建好,等待
				} catch (InterruptedException e) {
				}
			}
		}
		return mLooper;
	}

	/**
	 * Ask the currently running looper to quit. If the thread has not been
	 * started or has finished (that is if {@link #getLooper} returns null),
	 * then false is returned. Otherwise the looper is asked to quit and true is
	 * returned.
	 */
	public boolean quit() {
		Looper looper = getLooper();
		if (looper != null) {
			looper.quit();
			return true;
		}
		return false;
	}

	/**
	 * Returns the identifier of this thread. See Process.myTid().
	 */
	public int getThreadId() {
		return mTid;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值