【问题标题】:What is the difference between @UiThread and @MainThread annotation in Android?Android中@UiThread和@MainThread注解有什么区别?
【发布时间】:2020-11-19 22:56:54
【问题描述】:

我认为@UiThread 和@MainThread 是同一个东西。

【问题讨论】:

  • 我不明白@MainThread注解是什么,它的用法是什么。你能解释一下吗。这会很有帮助。
  • 注释如何“检查”?抛出异常? Thread annotations check if a method is called from

标签: android annotations


【解决方案1】:

@MainThread 是您启动应用程序时开始运行的第一个线程

@UiThread 从主线程开始渲染用户界面

同样来自 Android 文档

注意:@MainThread 和@UiThread 注释是可互换的,因此这些注释允许来自任一线程类型的方法调用。

https://developer.android.com/tools/debugging/annotations.html#thread-annotations

【讨论】:

  • 这是同一个线程。他们只是将其命名为 UI 线程,因为这是 UI 渲染发生的地方,并且实现可能在未来发生变化。
  • 这不是 100% 正确的,不仅仅是命名不同,正如文档所说: > 注意: 通常,应用程序的 UI 线程也是主线程。但是,在特殊情况下,应用的 UI 线程可能不是它的主线程;欲了解更多信息,请参阅Thread annotations
  • 注释如何“检查”?抛出异常? Thread annotations check if a method is called from
【解决方案2】:

更完整的解释:在系统应用程序在不同线程上具有多个视图的情况下,UI 线程可能与主线程不同。因此,您应使用@UiThread 注释与应用的视图层次结构 关联的方法,并仅使用@MainThread 注释与应用的生命周期 关联的方法。

【讨论】:

    猜你喜欢
    • 2014-12-29
    • 1970-01-01
    • 2019-01-08
    • 2020-08-10
    • 2011-10-13
    • 2019-05-31
    • 2012-02-18
    • 2015-01-01
    相关资源
    最近更新 更多