应用程序子线程消息循环模型
/*在Java框架中,如果我们想在当前应用程序中创建一个子线程,一般就是通过自己实现一个类,
这个类继承于Thread类,然后重载Thread类的run函数,把我们想要在这个子线程执行的任务都
放在这个run函数里面实现。最后实例这个自定义的类,并且调用它的start函数,这样一个子线程就创建好了,
并且会调用这个自定义类的run函数。但是当这个run函数执行完成后,子线程也就结束了,它没有消息循环的概念。
*/
Runnable
libcore/ojluni/src/main/java/java/lang/Runnable.java
public interface Runnable {/**
* When an object implementing interface of Runnable is used
* to create a thread, starting the thread causes the object's
* run method to be called in that separately executing
* thread.
*
* The general contract of the method run is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
Thread
/*** A <i>thread</i> is a thread of execution in a program. The Java
* Virtual Machine allows an application to have multiple threads of
* execution running concurrently.
*
* Every thread has a priority. Threads with higher priority are
* executed in preference to threads with lower priority. Each thread
* may or may not also be marked as a daemon. When code running in
* some thread creates a new Thread object, the new
* thread has its priority initially set equal to the priority of the
* creating thread, and is a daemon thread if and only if the
* creating thread is a daemon.
*
* When a Java Virtual Machine starts up, there is usually a single
* non-daemon thread (which typically calls the method named
* <code>main</code> of some designated class). The Java Virtual
* Machine continues to execute threads until either of the following
* occurs:
* <ul>
* <li>The <code>exit</code> method of class <code>Runtime</code> has been
* called and the security manager has permitted the exit operation
* to take place.
* <li>All threads that are not daemon threads have died, either by
* returning from the call to the <code>run</code> method or by
* throwing an exception that propagates beyond the <code>run</code>
* method.
* </ul>
* <p>
* There are two ways to create a new thread of execution. One is to
* declare a class to be a subclass of <code>Thread</code>. This
* subclass should override the <code>run</code> method of class
* <code>Thread</code>. An instance of the subclass can then be
* allocated and started. For example, a thread that computes primes
* larger than a stated value could be written as follows:
*
* class PrimeThread extends Thread {
* long minPrime;
* PrimeThread(long minPrime) {
* this.minPrime = minPrime;
* }
*
* public void run() {
* // compute primes larger than minPrime
* . . .
* }
* }
*
* <p>
* The following code would then create a thread and start it running:
*
* PrimeThread p = new PrimeThread(143);
* p.start();
*
* <p>
* The other way to create a thread is to declare a class that
* implements the <code>Runnable</code> interface. That class then
* implements the <code>run</code> method. An instance of the class can
* then be allocated, passed as an argument when creating
* <code>Thread</code>, and started. The same example in this other
* style looks like the following:
*
* class PrimeRun implements Runnable {
* long minPrime;
* PrimeRun(long minPrime) {
* this.minPrime = minPrime;
* }
*
* public void run() {
* // compute primes larger than minPrime
* . . .
* }
* }
* <p>
* The following code would then create a thread and start it running:
*
* PrimeRun p = new PrimeRun(143);
* new Thread(p).start();
*
* <p>
* Every thread has a name for identification purposes. More than
* one thread may have the same name. If a name is not specified when
* a thread is created, a new name is generated for it.
*/
使用HandlerThread创建子线程的例子
class VoiceAssistantHandler extends Handler {public VoiceAssistantHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
Utils.logD(TAG, "handleMessage");
switch (msg.what) {
case MSG_SUBMIT_FEEDBACK:
submit();
break;
}
}
}
@Override
public void onCreate() {
//创建HandlerThread with thread name
HandlerThread handlerThread = new HandlerThread("voiceAssistantHanler");
//启动thread
handlerThread.start();
mServiceLooper = handlerThread.getLooper();
//创建handler
mHandler = new VoiceAssistantHandler(handlerThread.getLooper());
}
void sendMsgToTarget(int msgNum) {
//用Handler构建Message,sendToTarget,对应的Handle处理message
Message msg = mHandler.obtainMessage(msgNum);
msg.sendToTarget();
}