应该了解的知识:
1.什么是进程、什么是线程
2.为什么要使用多线程
3.Handler机制
4.Handler原理
什么是进程、什么是线程
进程是一个程序的完全执行,包括了程序和程序需要的资源,是分配资源的基本单位;线程是独立运行和独立调度的基本单位,一边听歌一边打字一边玩游戏看似是同时进行实际上是CPU运行时候,快速切换资源,让用户感觉是同时进行的,我们可以将线程理解为程序中的一段可执行的代码块。
为什么要使用多线程--》可以参考别人的专门理解的理解:http://blog.youkuaiyun.com/itjavawfc/article/details/50360776
2)并行操作时使用线程,如C/S架构的服务器端并发线程响应用户的请求。
3)多CPU系统中,使用线程提高CPU利用率
4)改善程序结构。一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。
/**
* 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.
*
* <p>Scheduling messages is accomplished with the
* {@link #post}, {@link #postAtTime(Runnable, long)},
* {@link #postDelayed}, {@link #sendEmptyMessage},
* {@link #sendMessage}, {@link #sendMessageAtTime}, and
* {@link #sendMessageDelayed} methods. The <em>post</em> versions allow
* you to enqueue Runnable objects to be called by the message queue when
* they are received; the <em>sendMessage</em> versions allow you to enqueue
* a {@link Message} object containing a bundle of data that will be
* processed by the Handler's {@link #handleMessage} method (requiring that
* you implement a subclass of Handler).
*
* <p>When posting or sending to a Handler, you can either
* allow the item to be processed as soon as the message queue is ready
* to do so, or specify a delay before it gets processed or absolute time for
* it to be processed. The latter two allow you to implement timeouts,
* ticks, and other timing-based behavior.
*
* <p>When a
* process is created for your application, its main thread is dedicated to
* running a message queue that takes care of managing the top-level
* application objects (activities, broadcast receivers, etc) and any windows
* they create. You can create your own threads, and communicate back with
* the main application thread through a Handler. This is done by calling
* the same <em>post</em> or <em>sendMessage</em> methods as before, but from
* your new thread. The given Runnable or Message will then be scheduled
* in the Handler's message queue and processed when appropriate.
*/
/**Initialize the current thread as a looper.
* This gives you a chance to create handlers that then reference
* this looper, before actually starting the loop. Be sure to call
* {@link #loop()} after calling this method, and end it by calling
* {@link #quit()}.
*/
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
sThreadLocal.set(new Looper(quitAllowed))
}
我们在看看Looper(quitAllowed)源码:
/**
*Return the {@link MessageQueue} object associated with the current
* thread. This must be called from a thread running a Looper, or a
* NullPointerException will be thrown.
*/
public static @NonNull MessageQueue myQueue() {
return myLooper().mQueue;
}
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue;
// Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
msg.target.dispatchMessage(msg);
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
// Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
}
msg.recycleUnchecked();
}
}
该方法的解释:
Run the message queue in this thread. Be sure to call
/** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */ public static @Nullable Looper myLooper() { return sThreadLocal.get(); }
进入无线循环while(true):
for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. return; }
可以看看next(),当message为空,则线程挂起。
msg.target.dispatchMessage(),完成消息的处理。
msg.target.dispatchMessage(msg);要看看里面的源码:/** * Handle system messages here. */ public void dispatchMessage(Message msg) { if (msg.callback != null) { handleCallback(msg); } else { if (mCallback != null) { if (mCallback.handleMessage(msg)) { return; } } handleMessage(msg); } }
处理完消息后ongmsg.recycleUnchecked();回收Message对象占用的系统资源。
MessageQueue消息队列:消息读/取不能同时进行,有枷锁机制
/**
* Low-level class holding the list of messages to be dispatched by a
* {@link Looper}. Messages are not added directly to a MessageQueue,
* but rather through {@link Handler} objects associated with the Looper.
*
* <p>You can retrieve the MessageQueue for the current thread with
* {@link Looper#myQueue() Looper.myQueue()}.
*/
public final class MessageQueue {
看源码:保存message的list【容器】,message不是直接添加道MessageQueue中取,通过looper对象来的。
Handler:
在Looper.looper()中取出消息后,回掉msg.target对象的handleMessage()添加函数,而msg.target的类型正是handler。
Handler处理消息地方:
/**
* Handle system messages here.
*/
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
实际项目中的运用:三点要理解
第一种方式:
先:重写创建handler对象,重写handler的handler的handlerMessage()方法
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
//消息接收到了
int what = msg.what;
switch (what) {
case 1:
String text = (String) msg.obj;
ts.setText(text);
break;
case 2:
//刷新其他UI
break;
default:
break;
}
};
};
其次创建消息:
Message msg = new Message();//初始化方式
msg_ob.obj = "你家的牛在我菜园吃菜";
msg_ob.what = 1;//企业开发用常量
handler.sendMessage(msg);
下面是否封装的一个HandlerUtils工具:
public class MsgUtils {
static public void SendMSG(Handler handler,Object obj,int what,int arg1){
Message msg = new Message();
msg.obj = obj;
msg.what = what;
msg.arg1 = arg1;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,Object obj,int what){
Message msg = new Message();
msg.obj = obj;
msg.what = what;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,int what){
Message msg = new Message();
msg.what = what;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,int what,int arg1){
Message msg = new Message();
msg.what = what;
msg.arg1 = arg1;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,Object obj){
Message msg = new Message();
msg.obj = obj;
handler.sendMessage(msg);
}
static public void SendMSG(Handler handler,int what,Object obj,int arg1){
Message msg = new Message();
msg.obj = obj;
msg.arg1 = arg1;
msg.what = what;
handler.sendMessage(msg);
}
}
第二种方式:获取message消息方式不一样
Message msg_ob = handler.obtainMessage();//使用了池的概念
msg_ob.obj = "你家的牛在我菜园吃菜";
msg_ob.what = 1;//企业开发用常量
msg_ob.sendToTarget();
直接通过handler的obtainMessage()方法使用了池的概念
源码如下:
/**
* Returns a new {@link android.os.Message Message} from the global message pool. More efficient than
* creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
* If you don't want that facility, just call Message.obtain() instead.
*/
public final Message obtainMessage()
{
return Message.obtain(this);
}
/**
* Same as {@link #obtainMessage()}, except that it also sets the what member of the returned Message.
*
* @param what Value to assign to the returned Message.what field.
* @return A Message from the global message pool.
*/
public final Message obtainMessage(int what)
{
return Message.obtain(this, what);
}
/**
* Same as {@link #obtain()}, but sets the value for the <em>target</em> member on the Message returned.
* @param h Handler to assign to the returned Message object's <em>target</em> member.
* @return A Message object from the global pool.
*/
public static Message obtain(Handler h) {
Message m = obtain();
m.target = h;
return m;
}
第三种方式,实现一个ruannable接口,然后将这个线程作为消息发送:
private Runnable run_handler = new Runnable() {
public void run() {
i++;
ts.setText("runnable 刷新UI"+i+"次");
handler.postDelayed(run_handler, 100);
}
};
这种方式的好处是随时添加删除:
handler.post(run_handler);
handler.removeCallbacks(run_handler);//停止循环
/**
* Causes the Runnable r to be added to the message queue.
* The runnable will be run on the thread to which this handler is
* attached.
*
* @param r The Runnable that will be executed.
*
* @return Returns true if the Runnable was successfully placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting.
*/
public final boolean post(Runnable r)
{
return sendMessageDelayed(getPostMessage(r), 0);
}
/**
* Causes the Runnable r to be added to the message queue, to be run
* at a specific time given by <var>uptimeMillis</var>.
* <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
* Time spent in deep sleep will add an additional delay to execution.
* The runnable will be run on the thread to which this handler is attached.
*
* @param r The Runnable that will be executed.
* @param uptimeMillis The absolute time at which the callback should run,
* using the {@link android.os.SystemClock#uptimeMillis} time-base.
*
* @return Returns true if the Runnable was successfully placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting. Note that a
* result of true does not mean the Runnable will be processed -- if
* the looper is quit before the delivery time of the message
* occurs then the message will be dropped.
*/
public final boolean postAtTime(Runnable r, long uptimeMillis)
{
return sendMessageAtTime(getPostMessage(r), uptimeMillis);
}