深入Android线程的相关问题解惑
程序员文章站
2024-02-09 08:44:58
一个应用只有一个线程在默认情况下,更确切的讲一个进程中只有一个线程,这跟其他语言,比如c/c++,java等是一致。也就是说在android应用程序里面一个进程只有一个线程...
一个应用只有一个线程
在默认情况下,更确切的讲一个进程中只有一个线程,这跟其他语言,比如c/c++,java等是一致。也就是说在android应用程序里面一个进程只有一个线程,所有组件都运行在一个线程里面!
当应用程序启动时,系统会为其创建一个进程,也会创建一个线程名字叫做main,所有其所属组件的创建,系统事件的处理,系统的回调等一切应用相关的事情都运行在此名叫main的线程中。此线程即为常说的主线程(main thread)。俗称的ui线程(ui thread)也是它,因为只有主线程可以操作ui相关的事情,所以有人把主线程也称作ui线程,但这并不是正确的说法,因为service所属的线程也可以操作toast,但是service并没有ui。为什么非主线程不能操作ui呢?因为对ui操作常常会引发系统的回调,所以如果允许第三线程来操作可能会引发系统回调的紊乱,进而会打乱整个框架的时序!
这里要特别注意的就是同一个进程中的所有组件运行在同一个线程中,activiy,service,boradcastreceiver和contentprovider都运行在主线程中。
最容易引起误解的就是service,文档和常识都会认为service是放在后台用于操作费时运算的,但是实则不然,如果你在service中做费时操作,同样会引发臭名昭著的anr(application not responding)。所以如果想把service当做一个server,必须在service用handlerthread或thread创建一个worker线程!
activity也是一样的,你startactivity()后,开启了一个新的activity,但它们都运行在同一个线程中,所以你还是不能在原activity中做费时操作!也即在调用startactivity()开启了一个新的activity后,或者在onpause(), onstop(), ondestroy()中做费时操作会引发anr。
对于contentprovider也是一样的,如果跟其他组件在同一进程内,那么调用contentresolver的方法会相当于直接调用contentprovider的方法。如果是在另外一个进程中,虽是通过ipc,但也是同步的,因为ibinder的同步的,也即调用contentresolver时会把调用者的进程挂起,等待contentprovider的进程操作结束,再把结果传给调用者进程!所以,如果contentprovider中有费时操作,或者会同步锁数据库等,也一定要注意anr的发生!
所以一定要记住:一个进程只有一个线程,所有组件都运行在主线程中。
因此,如果有费时操作,必须要创建worker线程!
下面有一个小实例,一个应用中有五个组件:2个activity,一个service,一个contentprovider和一个broadcastreceiver。在每个组件的方法中都有打印所属线程信息,另外对于activity,service和contentprovider如果做费时操作会引发anr,对于broadcastreceiver更是如此,这个大家都懂得的!
public class activitydemo extends activity {
private static final string tag = "activitydemo";
private handler mmainhandler = new handler(new handler.callback() {
public boolean handlemessage(message msg) {
dumpthreadinfo();
return false;
}
});
@override
protected void oncreate(bundle savedinstancestate) {
dumpthreadinfo();
super.oncreate(savedinstancestate);
// add four buttons
linearlayout layout = new linearlayout(getapplication());
layout.setorientation(linearlayout.vertical);
button startservice = new button(getapplication());
startservice.settext("start a service");
startservice.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
intent i = new intent(getapplication(), servicedemo.class);
startservice(i);
}
});
layout.addview(startservice);
button startanother = new button(getapplication());
startanother.settext("start another activity");
startanother.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
intent i = new intent(getapplication(), anotheractivity.class);
startservice(i);
}
});
layout.addview(startanother);
button startcontentprovider = new button(getapplication());
startcontentprovider.settext("start a contentprovider");
startcontentprovider.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
getcontentresolver().query(contentproviderdemo.content_uri, null, null, null, null);
}
});
layout.addview(startcontentprovider);
button startreceiver = new button(getapplication());
startreceiver.settext("start a broadcastreceiver");
startreceiver.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
intent i = new intent("android.action.start_broadcastreceiver_demo");
sendbroadcast(i);
}
});
layout.addview(startreceiver);
setcontentview(layout);
mmainhandler.sendemptymessagedelayed(0, 500);
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
log.e(tag, " " + getmainlooper());
}
}
public class anotheractivity extends activity {
private static final string tag = "anotheractivity";
private handler mmainhandler = new handler(getmainlooper(), new handler.callback() {
public boolean handlemessage(message msg) {
// this will cause anr
log.e(tag, "you know what this is very slow slow slow slow");
systemclock.sleep(20 * 1000);
dumpthreadinfo();
return false;
}
});
@override
protected void oncreate(bundle savedinstancestate) {
dumpthreadinfo();
super.oncreate(savedinstancestate);
settitle("this is another activity");
mmainhandler.sendemptymessagedelayed(0, 500);
}
@override
protected void ondestroy() {
dumpthreadinfo();
super.ondestroy();
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
log.e(tag, " " + getmainlooper());
}
}
public class servicedemo extends service {
private handler mmainhandler = new handler(new handler.callback() {
public boolean handlemessage(message msg) {
// this will cause anr, too
log.e(tag, "this is very slow you know, slow slow");
systemclock.sleep(20 * 1000);
dumpthreadinfo();
return false;
}
});
private static final string tag = "servicedemo";
@override
public ibinder onbind(intent arg0) {
dumpthreadinfo();
return null;
}
@override
public void oncreate() {
dumpthreadinfo();
super.oncreate();
mmainhandler.sendemptymessagedelayed(0, 500);
}
@override
public int onstartcommand(intent intent, int flags, int startid) {
dumpthreadinfo();
return super.onstartcommand(intent, flags, startid);
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
log.e(tag, " " + getmainlooper());
}
}
public class contentproviderdemo extends contentprovider {
public static final uri content_uri = uri.parse("content://com.hilton.effectiveandroid.app/content");
private static final string tag = "contentproviderdemo";
@override
public int delete(uri arg0, string arg1, string[] arg2) {
dumpthreadinfo();
return 0;
}
@override
public uri insert(uri uri, contentvalues values) {
dumpthreadinfo();
return null;
}
@override
public boolean oncreate() {
dumpthreadinfo();
return false;
}
@override
public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) {
dumpthreadinfo();
// it will cause anr of course
log.e(tag, "this is very slow, you know that");
systemclock.sleep(20 * 1000);
return null;
}
@override
public int update(uri uri, contentvalues values, string selection, string[] selectionargs) {
dumpthreadinfo();
return 0;
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
}
@override
public string gettype(uri arg0) {
return null;
}
}
public class broadcastreceiverdemo extends broadcastreceiver {
private static final string tag = "broadcastreceiverdemo";
@override
public void onreceive(context context, intent intent) {
log.e(tag, "intent is " + intent);
dumpthreadinfo();
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
}
}
在默认情况下,更确切的讲一个进程中只有一个线程,这跟其他语言,比如c/c++,java等是一致。也就是说在android应用程序里面一个进程只有一个线程,所有组件都运行在一个线程里面!
当应用程序启动时,系统会为其创建一个进程,也会创建一个线程名字叫做main,所有其所属组件的创建,系统事件的处理,系统的回调等一切应用相关的事情都运行在此名叫main的线程中。此线程即为常说的主线程(main thread)。俗称的ui线程(ui thread)也是它,因为只有主线程可以操作ui相关的事情,所以有人把主线程也称作ui线程,但这并不是正确的说法,因为service所属的线程也可以操作toast,但是service并没有ui。为什么非主线程不能操作ui呢?因为对ui操作常常会引发系统的回调,所以如果允许第三线程来操作可能会引发系统回调的紊乱,进而会打乱整个框架的时序!
这里要特别注意的就是同一个进程中的所有组件运行在同一个线程中,activiy,service,boradcastreceiver和contentprovider都运行在主线程中。
最容易引起误解的就是service,文档和常识都会认为service是放在后台用于操作费时运算的,但是实则不然,如果你在service中做费时操作,同样会引发臭名昭著的anr(application not responding)。所以如果想把service当做一个server,必须在service用handlerthread或thread创建一个worker线程!
activity也是一样的,你startactivity()后,开启了一个新的activity,但它们都运行在同一个线程中,所以你还是不能在原activity中做费时操作!也即在调用startactivity()开启了一个新的activity后,或者在onpause(), onstop(), ondestroy()中做费时操作会引发anr。
对于contentprovider也是一样的,如果跟其他组件在同一进程内,那么调用contentresolver的方法会相当于直接调用contentprovider的方法。如果是在另外一个进程中,虽是通过ipc,但也是同步的,因为ibinder的同步的,也即调用contentresolver时会把调用者的进程挂起,等待contentprovider的进程操作结束,再把结果传给调用者进程!所以,如果contentprovider中有费时操作,或者会同步锁数据库等,也一定要注意anr的发生!
所以一定要记住:一个进程只有一个线程,所有组件都运行在主线程中。
因此,如果有费时操作,必须要创建worker线程!
实例
下面有一个小实例,一个应用中有五个组件:2个activity,一个service,一个contentprovider和一个broadcastreceiver。在每个组件的方法中都有打印所属线程信息,另外对于activity,service和contentprovider如果做费时操作会引发anr,对于broadcastreceiver更是如此,这个大家都懂得的!
复制代码 代码如下:
public class activitydemo extends activity {
private static final string tag = "activitydemo";
private handler mmainhandler = new handler(new handler.callback() {
public boolean handlemessage(message msg) {
dumpthreadinfo();
return false;
}
});
@override
protected void oncreate(bundle savedinstancestate) {
dumpthreadinfo();
super.oncreate(savedinstancestate);
// add four buttons
linearlayout layout = new linearlayout(getapplication());
layout.setorientation(linearlayout.vertical);
button startservice = new button(getapplication());
startservice.settext("start a service");
startservice.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
intent i = new intent(getapplication(), servicedemo.class);
startservice(i);
}
});
layout.addview(startservice);
button startanother = new button(getapplication());
startanother.settext("start another activity");
startanother.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
intent i = new intent(getapplication(), anotheractivity.class);
startservice(i);
}
});
layout.addview(startanother);
button startcontentprovider = new button(getapplication());
startcontentprovider.settext("start a contentprovider");
startcontentprovider.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
getcontentresolver().query(contentproviderdemo.content_uri, null, null, null, null);
}
});
layout.addview(startcontentprovider);
button startreceiver = new button(getapplication());
startreceiver.settext("start a broadcastreceiver");
startreceiver.setonclicklistener(new view.onclicklistener() {
public void onclick(view v) {
intent i = new intent("android.action.start_broadcastreceiver_demo");
sendbroadcast(i);
}
});
layout.addview(startreceiver);
setcontentview(layout);
mmainhandler.sendemptymessagedelayed(0, 500);
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
log.e(tag, " " + getmainlooper());
}
}
复制代码 代码如下:
public class anotheractivity extends activity {
private static final string tag = "anotheractivity";
private handler mmainhandler = new handler(getmainlooper(), new handler.callback() {
public boolean handlemessage(message msg) {
// this will cause anr
log.e(tag, "you know what this is very slow slow slow slow");
systemclock.sleep(20 * 1000);
dumpthreadinfo();
return false;
}
});
@override
protected void oncreate(bundle savedinstancestate) {
dumpthreadinfo();
super.oncreate(savedinstancestate);
settitle("this is another activity");
mmainhandler.sendemptymessagedelayed(0, 500);
}
@override
protected void ondestroy() {
dumpthreadinfo();
super.ondestroy();
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
log.e(tag, " " + getmainlooper());
}
}
复制代码 代码如下:
public class servicedemo extends service {
private handler mmainhandler = new handler(new handler.callback() {
public boolean handlemessage(message msg) {
// this will cause anr, too
log.e(tag, "this is very slow you know, slow slow");
systemclock.sleep(20 * 1000);
dumpthreadinfo();
return false;
}
});
private static final string tag = "servicedemo";
@override
public ibinder onbind(intent arg0) {
dumpthreadinfo();
return null;
}
@override
public void oncreate() {
dumpthreadinfo();
super.oncreate();
mmainhandler.sendemptymessagedelayed(0, 500);
}
@override
public int onstartcommand(intent intent, int flags, int startid) {
dumpthreadinfo();
return super.onstartcommand(intent, flags, startid);
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
log.e(tag, " " + getmainlooper());
}
}
复制代码 代码如下:
public class contentproviderdemo extends contentprovider {
public static final uri content_uri = uri.parse("content://com.hilton.effectiveandroid.app/content");
private static final string tag = "contentproviderdemo";
@override
public int delete(uri arg0, string arg1, string[] arg2) {
dumpthreadinfo();
return 0;
}
@override
public uri insert(uri uri, contentvalues values) {
dumpthreadinfo();
return null;
}
@override
public boolean oncreate() {
dumpthreadinfo();
return false;
}
@override
public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) {
dumpthreadinfo();
// it will cause anr of course
log.e(tag, "this is very slow, you know that");
systemclock.sleep(20 * 1000);
return null;
}
@override
public int update(uri uri, contentvalues values, string selection, string[] selectionargs) {
dumpthreadinfo();
return 0;
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
}
@override
public string gettype(uri arg0) {
return null;
}
}
复制代码 代码如下:
public class broadcastreceiverdemo extends broadcastreceiver {
private static final string tag = "broadcastreceiverdemo";
@override
public void onreceive(context context, intent intent) {
log.e(tag, "intent is " + intent);
dumpthreadinfo();
}
public void dumpthreadinfo() {
thread.dumpstack();
log.e(tag, thread.currentthread().tostring());
}
}