应用程序子线程消息循环模型
/*
在Java框架中,如果我们想在当前应用程序中创建一个子线程,一般就是通过自己实现一个类,
这个类继承于Thread类,然后重载Thread类的run函数,把我们想要在这个子线程执行的任务都
放在这个run函数里面实现。最后实例这个自定义的类,并且调用它的start函数,这样一个子线程就创建好了,
并且会调用这个自定义类的run函数。但是当这个run函数执行完成后,子线程也就结束了,它没有消息循环的概念。
*/
Thread类的继承关系和构造函数
Class Thread implements Runnable {
public Thread(Runnalbe target) {
//构造函数的必要参数是Runnable, 最终执行的是Runnable
}
}
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.
//不管那种方法,最终都是用Runnable创建thread 并调用thread.star(), 这种使用thread的特点是执行完Runnable就销毁thread (one time)
*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创建子线程的例子
HandlerThread
/** * 1]Handy class for starting a new thread that has a looper. 2]The looper can then be * used to create handler classes. Note that 3]start() must still be called. */ public class HandlerThread extends Thread {}
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();
//由thread的Looper创建handler
mHandler = new VoiceAssistantHandler(handlerThread.getLooper());
}
void sendMsgToTarget(int msgNum) {
//用Handler还提供构建Message的函数,sendToTarget,对应的Handle处理message
Message msg = mHandler.obtainMessage(msgNum);
msg.sendToTarget();
}
Handler
/** * A Handler allows you to send and process {@link Message} and Runnable * objects associated with a thread's {@link MessageQueue}. Each Handler * instance is associated with a single thread and that thread's message * queue. When you create a new Handler, it is bound to the thread / * message queue of the thread that is creating it -- from that point on, * it will deliver messages and runnables to that message queue and execute * them as they come out of the message queue. * * <p>There are two main uses for a Handler: (1) to schedule messages and * runnables to be executed as some point in the future; and (2) to enqueue * an action to be performed on a different thread than your own.*/
Message使怎样找到对应处理函数handler的
frameworks/base/core/java/android/os/Handler.java
把处理Message的target:this传入Message
public final Message obtainMessage(int what)
{
return Message.obtain(this, what);
}
frameworks/base/core/java/android/os/Message.java
public static Message obtain(Handler h, int what) {
Message m = obtain();
m.target = h;
m.what = what;
return m;
}
最后调用sendToTarget(){
target.sendMessage(this);//此时target已经被赋值为,创建Message时所在的Handler
}