android-annotations - Android 注释 @UiThread 竞争条件?

标签 android-annotations

@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/

相关文章:

java - 如何使用 android 注释和 resttemplate 在 POST 请求正文中发送 x-www-form-urlencoded

Android Annotations + RestTemplate - 获取响应 xml 作为字符串

AndroidAnnotations 和 EventBus

java - Java中如何扩展注解?

Android 注释和新的 Android 数据绑定(bind)

Android + Groovy + Android 注解

android - 使用 Android Studio(测试版)0.8.4 配置 Android Annotations v3.0.1

java - 对同一按钮使用触摸和单击注释

AndroidAnnotations - ViewById 不能用于私有(private)元素

java - setResult 不适用于@EActivity