@UiThread
void foo() {
...some animations...
};
@UiThread
void bar() {
xyz();
};
@UiThread
void xyz() {
foo();
};
如果我在应用程序中使用许多这些嵌套 @UiThread 方法,我会遇到麻烦吗?
因此我可以获得一些竞争条件吗?
它只会影响性能吗?
最佳答案
@UIThread
将生成一个使用 Handler
来执行代码的方法,如下所示:
private Handler handler_ = new Handler();
public void bar() {
handler_.post(new Runnable() {
...
}
}
如果我们仔细看看 Handler's javadoc ,我们可以看到这段:
A Handler allows you to send and process Message and Runnable objects associated with a thread's 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.
Handler是在类实例化时创建的,因此它将链接到主线程。由于每次调用 @UIThread
注解的方法都会排队到该 Handler 中,因此每个方法都会依次执行。
所以这些方法中不应该有任何竞争条件。由于这些方法将在主线程上执行,因此您只需进行 UI 处理。
希望我能回答您所有的问题。
关于android-annotations - Android 注释 @UiThread 竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15398964/