深入Android Handler与线程间通信ITC的详解
程序员文章站
2023-12-09 23:02:45
在《android handler之消息循环的深入解析》中谈到了handler是用于操作线程内部的消息队列,所以handler可以用来线程间通信itc,这种方式更加安全和高...
在《android handler之消息循环的深入解析》中谈到了handler是用于操作线程内部的消息队列,所以handler可以用来线程间通信itc,这种方式更加安全和高效,可以大大减少同步的烦恼,甚至都可以不用syncrhonized。
线程间通讯itc
正常情况下函数调用栈都会生存在同一个线程内,想要把执行逻辑交换到其他线程可以新建一个thread,然后start()。另外一种方法就是用itc,也即用消息队列来实现,线程需要把执行逻辑交到其他线程时就向另外的线程的消息队列发送一个消息,发送消息后函数就此结束返回,调用栈也停止。当消息队列中有了消息时,线程会被唤醒来执行处理消息,从而把执行逻辑从一个线程转到另外一个线程。这就实现了线程间的通信itc,与进行间通讯ipc有十分类似的思想。
通常的做法都是,在主线程创建一个handler,然后在新建线程中使用此handler与主线程通讯。因为主线程的消息队列已经建好,所以直接创建handler即可,新建的线程就可以直接使用。
有些情况,需要在多线程之间进行通信,这就要为每个线程都创建messagequeue和handler,只要线程能访问其他线程的handler就可以与之通信。
要正确的创建handler,因为handler要与线程绑定,所以在初始化handler的时候就要注意:
如果给handler指定looper对象new handler(looper),那么此handler便绑定到looper对象所在的线程中,handler的消息处理回调会在那个线程中执行。
如果创建线程时不指定looper对象,那么此handler绑定到创建此handler的线程内,消息回调处理会在那个线程中执行,所以像下面的例子,如果这样写:
private class cookserver extends thread {
private handler mhandler = new handler() {
public void handlemessage(message msg) {
....
}
};
那么,此mhandler会与创建此cookerserver的线程绑定,handlemessage也会运行于其中。显然,如果是主线程调用new cookserver(),那么mhandler其实是运行在主线程中的。正确的写法应该是:
private class cookserver extends thread {
public void run() {
looper.prepare();
// or new handler(looper.mylooper())
private handler mhandler = new handler() {
public void handlemessage(message msg) {
....
}
};
handlerthread
如果要在一个线程中使用消息队列和handler,android api中已经有封装好了的一个类handlerthread,这个类已经做好了looper的初始化工作,你需要做的就是重写其onlooperprepared()方法,在其中创建handler:
private class deliverserver extends handlerthread {
private handler mhandler;
public deliverserver(string name) {
super(name);
}
@override
public void onlooperprepared() {
mhandler = new handler(getlooper()) {
public void handlemessage(message msg) {
.....
}
};
}
}
实例
此实例模拟了一个网络订餐系统,客户点击“submit order"来产生一个定单,主线程中负责收集定单,然后交由cookserver来制作,cookserver在制作完成后会交由deliverserver来把食物运送到客户,至此一个定单完成,同时cookserver和deliverserver会更新状态。
/**
* how to attach an handler to a thread:
* if you specify looper object to handler, i.e. new handler(looper), then the handler is attached to the thread owning
* the looper object, in which handlemessage() is executed.
* if you do not specify the looper object, then the handler is attached to the thread calling new handler(), in which
* handlemessage() is executed.
* in this example, for class cookserver or deliverserver, if you write this way:
* private class cookserver extends thread {
private handler mhandler;
private looper mlooper;
public cookserver() {
mhandler = new handler() {
@override
public void handlemessage(message msg) {
....
}
start();
}
* then mhandler is attached to thread calling new cookserver(), which is the main thread, so mhandler.handlemessage() will
* be executed in main thread.
* to attach mhandler to its own thread, you must put it in run(), or after mlooper is created. for our example, providing
* mlooper or not won't matter, because new handler() is called in run(), which is in a new thread.
*/
public class handleritcdemo extends listactivity {
private static final int cooking_started = 1;
private static final int cooking_done = 2;
private static final int delivering_started = 3;
private static final int order_done = 4;
private listview mlistview;
private static final string[] mfoods = new string[] {
"cubake",
"donut",
"eclaire",
"gingerbread",
"honeycomb",
"ice cream sanwitch",
"jelly bean",
};
private arraylist<string> morderlist;
private textview mgeneralstatus;
private button msubmitorder;
private static random mrandomer = new random(47);
private int mordercount;
private int mcookingcount;
private int mdeliveringcount;
private int mdonecount;
private handler mmainhandler = new handler() {
@override
public void handlemessage(message msg) {
switch (msg.what) {
case cooking_started:
mcookingcount++;
break;
case cooking_done:
mcookingcount--;
break;
case delivering_started:
mdeliveringcount++;
break;
case order_done:
mdeliveringcount--;
mdonecount++;
default:
break;
}
mgeneralstatus.settext(makestatuslabel());
}
};
private cookserver mcookserver;
private deliverserver mdeliverserver;
@override
protected void ondestroy() {
super.ondestroy();
if (mcookserver != null) {
mcookserver.exit();
mcookserver = null;
}
if (mdeliverserver != null) {
mdeliverserver.exit();
mdeliverserver = null;
}
}
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
mlistview = getlistview();
morderlist = new arraylist<string>();
mgeneralstatus = new textview(getapplication());
mgeneralstatus.settext(makestatuslabel());
msubmitorder = new button(getapplication());
msubmitorder.settext("submit order");
msubmitorder.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
string order = mfoods[mrandomer.nextint(mfoods.length)];
morderlist.add(order);
mordercount = morderlist.size();
mgeneralstatus.settext(makestatuslabel());
setadapter();
mcookserver.cook(order);
}
});
mlistview.addheaderview(mgeneralstatus);
mlistview.addfooterview(msubmitorder);
setadapter();
mcookserver = new cookserver();
mdeliverserver = new deliverserver("deliver server");
}
private string makestatuslabel() {
stringbuilder sb = new stringbuilder();
sb.append("total: ");
sb.append(mordercount);
sb.append(" cooking: ");
sb.append(mcookingcount);
sb.append(" delivering: ");
sb.append(mdeliveringcount);
sb.append(" done: ");
sb.append(mdonecount);
return sb.tostring();
}
private void setadapter() {
final listadapter adapter = new arrayadapter<string>(getapplication(), android.r.layout.simple_list_item_1, morderlist);
setlistadapter(adapter);
}
private class cookserver extends thread {
private handler mhandler;
private looper mlooper;
public cookserver() {
start();
}
@override
public void run() {
looper.prepare();
mlooper = looper.mylooper();
mhandler = new handler(mlooper, new handler.callback() {
public boolean handlemessage(message msg) {
new cooker((string) msg.obj);
return true;
}
});
looper.loop();
}
public void cook(string order) {
if (mlooper == null || mhandler == null) {
return;
}
message msg = message.obtain();
msg.obj = order;
mhandler.sendmessage(msg);
}
public void exit() {
if (mlooper != null) {
mlooper.quit();
mhandler = null;
mlooper = null;
}
}
}
private class cooker extends thread {
private string order;
public cooker(string order) {
this.order = order;
start();
}
@override
public void run() {
mmainhandler.sendemptymessage(cooking_started);
systemclock.sleep(mrandomer.nextint(50000));
mdeliverserver.deliver(order);
mmainhandler.sendemptymessage(cooking_done);
}
}
private class deliverserver extends handlerthread {
private handler mhandler;
public deliverserver(string name) {
super(name);
start();
}
@override
protected void onlooperprepared() {
super.onlooperprepared();
mhandler = new handler(getlooper(), new handler.callback() {
public boolean handlemessage(message msg) {
new deliver((string) msg.obj);
return true;
}
});
}
public void deliver(string order) {
if (mhandler == null || getlooper() == null) {
return;
}
message msg = message.obtain();
msg.obj = order;
mhandler.sendmessage(msg);
}
public void exit() {
quit();
mhandler = null;
}
}
private class deliver extends thread {
private string order;
public deliver(string order) {
this.order = order;
start();
}
@override
public void run() {
mmainhandler.sendemptymessage(delivering_started);
systemclock.sleep(mrandomer.nextint(50000));
mmainhandler.sendemptymessage(order_done);
}
}
}
线程间通讯itc
正常情况下函数调用栈都会生存在同一个线程内,想要把执行逻辑交换到其他线程可以新建一个thread,然后start()。另外一种方法就是用itc,也即用消息队列来实现,线程需要把执行逻辑交到其他线程时就向另外的线程的消息队列发送一个消息,发送消息后函数就此结束返回,调用栈也停止。当消息队列中有了消息时,线程会被唤醒来执行处理消息,从而把执行逻辑从一个线程转到另外一个线程。这就实现了线程间的通信itc,与进行间通讯ipc有十分类似的思想。
通常的做法都是,在主线程创建一个handler,然后在新建线程中使用此handler与主线程通讯。因为主线程的消息队列已经建好,所以直接创建handler即可,新建的线程就可以直接使用。
有些情况,需要在多线程之间进行通信,这就要为每个线程都创建messagequeue和handler,只要线程能访问其他线程的handler就可以与之通信。
要正确的创建handler,因为handler要与线程绑定,所以在初始化handler的时候就要注意:
如果给handler指定looper对象new handler(looper),那么此handler便绑定到looper对象所在的线程中,handler的消息处理回调会在那个线程中执行。
如果创建线程时不指定looper对象,那么此handler绑定到创建此handler的线程内,消息回调处理会在那个线程中执行,所以像下面的例子,如果这样写:
复制代码 代码如下:
private class cookserver extends thread {
private handler mhandler = new handler() {
public void handlemessage(message msg) {
....
}
};
那么,此mhandler会与创建此cookerserver的线程绑定,handlemessage也会运行于其中。显然,如果是主线程调用new cookserver(),那么mhandler其实是运行在主线程中的。正确的写法应该是:
复制代码 代码如下:
private class cookserver extends thread {
public void run() {
looper.prepare();
// or new handler(looper.mylooper())
private handler mhandler = new handler() {
public void handlemessage(message msg) {
....
}
};
handlerthread
如果要在一个线程中使用消息队列和handler,android api中已经有封装好了的一个类handlerthread,这个类已经做好了looper的初始化工作,你需要做的就是重写其onlooperprepared()方法,在其中创建handler:
复制代码 代码如下:
private class deliverserver extends handlerthread {
private handler mhandler;
public deliverserver(string name) {
super(name);
}
@override
public void onlooperprepared() {
mhandler = new handler(getlooper()) {
public void handlemessage(message msg) {
.....
}
};
}
}
实例
此实例模拟了一个网络订餐系统,客户点击“submit order"来产生一个定单,主线程中负责收集定单,然后交由cookserver来制作,cookserver在制作完成后会交由deliverserver来把食物运送到客户,至此一个定单完成,同时cookserver和deliverserver会更新状态。
复制代码 代码如下:
/**
* how to attach an handler to a thread:
* if you specify looper object to handler, i.e. new handler(looper), then the handler is attached to the thread owning
* the looper object, in which handlemessage() is executed.
* if you do not specify the looper object, then the handler is attached to the thread calling new handler(), in which
* handlemessage() is executed.
* in this example, for class cookserver or deliverserver, if you write this way:
* private class cookserver extends thread {
private handler mhandler;
private looper mlooper;
public cookserver() {
mhandler = new handler() {
@override
public void handlemessage(message msg) {
....
}
start();
}
* then mhandler is attached to thread calling new cookserver(), which is the main thread, so mhandler.handlemessage() will
* be executed in main thread.
* to attach mhandler to its own thread, you must put it in run(), or after mlooper is created. for our example, providing
* mlooper or not won't matter, because new handler() is called in run(), which is in a new thread.
*/
public class handleritcdemo extends listactivity {
private static final int cooking_started = 1;
private static final int cooking_done = 2;
private static final int delivering_started = 3;
private static final int order_done = 4;
private listview mlistview;
private static final string[] mfoods = new string[] {
"cubake",
"donut",
"eclaire",
"gingerbread",
"honeycomb",
"ice cream sanwitch",
"jelly bean",
};
private arraylist<string> morderlist;
private textview mgeneralstatus;
private button msubmitorder;
private static random mrandomer = new random(47);
private int mordercount;
private int mcookingcount;
private int mdeliveringcount;
private int mdonecount;
private handler mmainhandler = new handler() {
@override
public void handlemessage(message msg) {
switch (msg.what) {
case cooking_started:
mcookingcount++;
break;
case cooking_done:
mcookingcount--;
break;
case delivering_started:
mdeliveringcount++;
break;
case order_done:
mdeliveringcount--;
mdonecount++;
default:
break;
}
mgeneralstatus.settext(makestatuslabel());
}
};
private cookserver mcookserver;
private deliverserver mdeliverserver;
@override
protected void ondestroy() {
super.ondestroy();
if (mcookserver != null) {
mcookserver.exit();
mcookserver = null;
}
if (mdeliverserver != null) {
mdeliverserver.exit();
mdeliverserver = null;
}
}
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
mlistview = getlistview();
morderlist = new arraylist<string>();
mgeneralstatus = new textview(getapplication());
mgeneralstatus.settext(makestatuslabel());
msubmitorder = new button(getapplication());
msubmitorder.settext("submit order");
msubmitorder.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
string order = mfoods[mrandomer.nextint(mfoods.length)];
morderlist.add(order);
mordercount = morderlist.size();
mgeneralstatus.settext(makestatuslabel());
setadapter();
mcookserver.cook(order);
}
});
mlistview.addheaderview(mgeneralstatus);
mlistview.addfooterview(msubmitorder);
setadapter();
mcookserver = new cookserver();
mdeliverserver = new deliverserver("deliver server");
}
private string makestatuslabel() {
stringbuilder sb = new stringbuilder();
sb.append("total: ");
sb.append(mordercount);
sb.append(" cooking: ");
sb.append(mcookingcount);
sb.append(" delivering: ");
sb.append(mdeliveringcount);
sb.append(" done: ");
sb.append(mdonecount);
return sb.tostring();
}
private void setadapter() {
final listadapter adapter = new arrayadapter<string>(getapplication(), android.r.layout.simple_list_item_1, morderlist);
setlistadapter(adapter);
}
private class cookserver extends thread {
private handler mhandler;
private looper mlooper;
public cookserver() {
start();
}
@override
public void run() {
looper.prepare();
mlooper = looper.mylooper();
mhandler = new handler(mlooper, new handler.callback() {
public boolean handlemessage(message msg) {
new cooker((string) msg.obj);
return true;
}
});
looper.loop();
}
public void cook(string order) {
if (mlooper == null || mhandler == null) {
return;
}
message msg = message.obtain();
msg.obj = order;
mhandler.sendmessage(msg);
}
public void exit() {
if (mlooper != null) {
mlooper.quit();
mhandler = null;
mlooper = null;
}
}
}
private class cooker extends thread {
private string order;
public cooker(string order) {
this.order = order;
start();
}
@override
public void run() {
mmainhandler.sendemptymessage(cooking_started);
systemclock.sleep(mrandomer.nextint(50000));
mdeliverserver.deliver(order);
mmainhandler.sendemptymessage(cooking_done);
}
}
private class deliverserver extends handlerthread {
private handler mhandler;
public deliverserver(string name) {
super(name);
start();
}
@override
protected void onlooperprepared() {
super.onlooperprepared();
mhandler = new handler(getlooper(), new handler.callback() {
public boolean handlemessage(message msg) {
new deliver((string) msg.obj);
return true;
}
});
}
public void deliver(string order) {
if (mhandler == null || getlooper() == null) {
return;
}
message msg = message.obtain();
msg.obj = order;
mhandler.sendmessage(msg);
}
public void exit() {
quit();
mhandler = null;
}
}
private class deliver extends thread {
private string order;
public deliver(string order) {
this.order = order;
start();
}
@override
public void run() {
mmainhandler.sendemptymessage(delivering_started);
systemclock.sleep(mrandomer.nextint(50000));
mmainhandler.sendemptymessage(order_done);
}
}
}